关闭

http https 下载

643人阅读 评论(0) 收藏 举报

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);
 }

 

 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:65350次
    • 积分:358
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:57篇
    • 译文:0篇
    • 评论:4条
    最新评论