posts - 71,  comments - 203,  trackbacks - 0

package com.aoran.util;

import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URL;

/**
 * @author riddlezhang 下载器
 */
public class DownloadUtil {
 private static final String TAG = "Downloader";

 /**
  * @return InputStream 下载
  */
 public static HttpURLConnection download(String url) {
  HttpURLConnection conn = null;
  try {
   String proxyHost = android.net.Proxy.getDefaultHost();
   if (proxyHost != null) {//如果是wap方式,要加网关
    java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(
      android.net.Proxy.getDefaultHost(), android.net.Proxy.getDefaultPort()));
    conn = (HttpURLConnection) new URL(url).openConnection(p);
   } else {
    conn = (HttpURLConnection) new URL(url).openConnection();
   }
   // conn.setReadTimeout(5000);
   conn.setConnectTimeout(10000);
   conn.setRequestMethod("GET");
   conn.setRequestProperty(
     "Accept",
     "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
   conn.setRequestProperty("Accept-Language", "zh-CN");
   conn.setRequestProperty("Referer", url);
   conn.setRequestProperty("Charset", "UTF-8");
   conn.setRequestProperty(
     "User-Agent",
     "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
   conn.setRequestProperty("Connection", "Keep-Alive");
   conn.connect();
   if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
    return conn;
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return null;
 }

}

posted on 2011-03-04 15:47  stay  阅读(2176)  评论(0编辑  收藏