http https 下载

来源:互联网 发布:数据交换前置机怎么做 编辑:程序博客网 时间:2024/05/17 15:37

readLen = inputStream.read(temp)) 返回-1

 

 

 public void downRss2() {
  long time1 = System.currentTimeMillis();
  InputStream inputStream = null;
  FileOutputStream fos = null;
  String filePath = path + "/voa/RSSlink.xml";

HttpClient httpClient = new DefaultHttpClient();
  HttpGet get = new HttpGet(baseUrl);
  try {
   fos = new FileOutputStream(filePath);

   // FakeX509TrustManager.allowAllSSL();
   HttpParams params = new BasicHttpParams();
   HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
   HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
   HttpProtocolParams.setUseExpectContinue(params, true);
   HttpProtocolParams
     .setUserAgent(
       params,
       "Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83) "
         + " AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1");
   // 超时设置
   /* 从连接池中取连接的超时时间 */
   ConnManagerParams.setTimeout(params, 1000);
   /* 连接超时 */
   HttpConnectionParams.setConnectionTimeout(params, 2000);
   /* 请求超时 */
   HttpConnectionParams.setSoTimeout(params, 4000);

   // 设置我们的HttpClient支持HTTP和HTTPS两种模式
   SchemeRegistry schReg = new SchemeRegistry();
   schReg.register(new Scheme("http", PlainSocketFactory
     .getSocketFactory(), 80));
   schReg.register(new Scheme("https", SSLSocketFactory
     .getSocketFactory(), 443));
     
   HttpResponse httpResponse = httpClient.execute(get);

   int responseCode = httpResponse.getStatusLine().getStatusCode();
   Log.d("zdj", "responseCode = " + responseCode);
   HttpEntity entity = httpResponse.getEntity();
   if (entity != null) {
    Log.d("zdj", "entity is not null");
    inputStream = entity.getContent();
    byte[] temp = new byte[512];
    int readLen = 0;
    while ((readLen = inputStream.read(temp)) > 0) {
     writelog("readlen  :  " + String.valueOf(readLen));
     fos.write(temp, 0, readLen);
    }
   }
  } catch (IOException e) {
   e.printStackTrace();
   dialog("downRss");

  } finally {
   try {
    if (inputStream != null) {
     inputStream.close();
    }
    if (fos != null) {
     fos.close();
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  /***************************************************************************************/
  File rss = new File(path + "/voa/RSSlink.xml");
  if (!rss.exists()) {
   String str = "RSSLink.xml下载失败";
   writelog(str);
  }
  /***************************************************************************************/
  long time2 = System.currentTimeMillis();
  String str = String.valueOf((time2 - time1) / 1000);
  writelog("RSSlink.xml文件下载时间:  " + str);
 }

 

 

 

 

原创粉丝点击