新手请教:android下的https请求文件下载问题

yj5723187 2014-10-14 07:48:50
例如链接地址:https://123.123.123.123/...../aaa.jpg
代码:

private int GetDownLoadFileSize() throws Exception{
HttpsURLConnection connection=null;

try{
SSLContext sslctxt = SSLContext.getInstance("SSL");

sslctxt.init(null, new TrustManager[]{new MyX509TrustManager()}, new java.security.SecureRandom());

if (sslctxt != null)
{
HttpsURLConnection.setDefaultSSLSocketFactory(sslctxt
.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(new MyHostnameVerifier());

connection=(HttpsURLConnection) this._DownLoadUrl.openConnection();


connection.setInstanceFollowRedirects(false);
connection.setDoOutput(true);
connection.setDoInput(true);

connection.setRequestMethod("GET");
connection.setRequestProperty("Cookie", StringUtil.vpnAuth.cookie);
connection.setRequestProperty("Content-Type","application/octet-stream");
connection.setRequestProperty("Connection", "Keep-Alive");
//connection.setRequestProperty("Accept-Encoding", "identity");
connection.connect();
}catch(Exception ex){
throw new Exception("打开url出错,详细信息:"+ex.getMessage());
}
//获取状态码,判断文件是否存在
if(connection.getResponseCode()!=200)
throw new Exception("要下载的文件不存在");

//返回文件的大小
return connection.getContentLength();
}

在 connection.getResponseCode() 获取状态码的时候就抛出 java.io.EOFException 的异常。。。

有做过这方面的朋友帮忙解答一下? 或者有类似的代码参考参考??
...全文
465 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
svenwang 2014-10-16
  • 打赏
  • 举报
回复
可以把url公布一下供别人测试你这段代码吗?
懒猪5205 2014-10-16
  • 打赏
  • 举报
回复
过来看看就知道了 ;
Hare_ 2014-10-15
  • 打赏
  • 举报
回复
代码看着没啥问题!
h1324694009 2014-10-15
  • 打赏
  • 举报
回复
在模拟器上遇到过,拿到真机上就不会有问题了。
h1324694009 2014-10-15
  • 打赏
  • 举报
回复
遇到过,在模拟器上会抛这个问题,但是在真机上就没有遇到,你放到真机上试一下。
yj5723187 2014-10-15
  • 打赏
  • 举报
回复
引用 3 楼 hjq2013 的回复:
代码看着没啥问题!
一样不行。。。 头疼
yj5723187 2014-10-15
  • 打赏
  • 举报
回复
引用 1 楼 h1324694009 的回复:
遇到过,在模拟器上会抛这个问题,但是在真机上就没有遇到,你放到真机上试一下。
我放真机上也不行啊... 能把你代码贴出来参考下吗? 你也是做过类似https协议的文件下载??

80,352

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧