HttpURLConnection getContentLength返回-1
2017年10月30日 20:09:42 yifan42421 阅读数:1703更多
个人分类: android
解决方法:
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Accept-Encoding", "identity");
urlConnection.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36");
全部源码:
-
try { -
//打开链接 -
URL url = new URL(strings[0]); -
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); -
//必须加上下面三句,要不total为-1 -
urlConnection.setRequestMethod("POST"); -
urlConnection.setRequestProperty("Accept-Encoding", "identity"); -
urlConnection.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"); -
if (200 == urlConnection.getResponseCode()) { -
Log.i(TAG, "200"); -
//得到输入流 -
InputStream is = urlConnection.getInputStream(); -
ByteArrayOutputStream baos = new ByteArrayOutputStream(); -
byte[] buffer = new byte[1024]; -
int len = 0; -
int count = 0; -
long total = urlConnection.getContentLength(); -
Log.i(TAG, "total = " + total); -
while (-1 != (len = is.read(buffer))) { -
baos.write(buffer, 0, len); -
count += len; -
publishProgress((int) ((count / (float) total) * 100)); -
Log.i(TAG, " baos = " + baos.toString("utf-8")); -
Thread.sleep(500); -
} -
Log.i(TAG, " baos = " + baos.toString("utf-8")); -
return baos.toString("utf-8"); -
} else { -
Log.i(TAG, " 系统错误 code = " + urlConnection.getResponseCode()); -
} -
} catch (MalformedURLException e) { -
Log.i(TAG, "URLException = " + e.toString()); -
e.printStackTrace(); -
} catch (IOException e) { -
Log.i(TAG, "IOException = " + e.toString()); -
e.printStackTrace(); -
} catch (InterruptedException e) { -
Log.i(TAG, "InterruptedException = " + e.toString()); -
e.printStackTrace(); -
}
参考链接:点击打开链接
获取网络大小使用的代码如下:
[java] view plain copy
- URL url=new URL("http://yinyueshiting.baidu.com/data2/music/123296359/13132725248400128.mp3?xcode=bce09e68531be7afafd0ef4fac9a741419c56da7f7cc7a9f");
- HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
- //根据响应获取文件大小
- int fileLength=urlcon.getContentLength(); //这里获取的是字节
- double fileLenM=Double.parseDouble(df.format((fileLength/1024.00)/1024.00)); //转为M
正常情况下能获取到 文件的大小
如果返回 -1,原因可能有很多种
首先排除 链接不正确或者服务器响应的问题 或者ip被屏蔽
假设我现在要获取
http://yinyueshiting.baidu.com/data2/music/123296359/13132725248400128.mp3?xcode=bce09e68531be7afafd0ef4fac9a741419c56da7f7cc7a9f
链接的mp3的大小
则把该链接在浏览器的下载任务中新建 尝试下载


如果能下载,则说明链接正确,服务器也没问题,ip也没被屏蔽也就是我们的代码有问题。
网上一些解决方案说是HttpURLConnection 使用 gzip方式获取 的原因。
加上代码
urlcon.setRequestProperty("Accept-Encoding", "identity");
可以解决
详见
Android getContentLength()为-1 解决方法
如果上述方法仍然不行
那继续往下分析
我们在谷歌浏览器中访问这个链接
查看它的请求头以及response 发现response中是有content-length的 一般都会有

那我们的代码问题到底出在哪呢
一般可能是请求头的问题 导致被服务器拒绝访问了
我的就是这个原因
加上User-agent 伪装一下 伪装成浏览器 后就可以了
[java] view plain copy
- URL url;
- url = new URL();
- HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
- urlcon.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36");
- urlcon.setConnectTimeout(15000);
- urlcon.setReadTimeout(15000);
- //根据响应获取文件大小
- fileLength=urlcon.getContentLength();
- System.out.println("get fileLen "+fileLength);
如果还不能解决 那就得用fidder工具好好分析一下了
通过fidder分析 我们发出的请求头 request跟浏览器发出的 request 有哪些区别
浏览器的request很容易获取
打开 fidder,fidder会自动获取到浏览器中输入的链接的 相关信息。

而我们代码 运行的request则需要在代码中 加 代理
指向本机的 fidder监听端口
工具----> fidder选项--->监听端口 可以查看端口

代码中加代理
[java] view plain copy
- String host = "127.0.0.1";
- String port = "8888";
- setProxy(host, port);
- public static void setProxy(String host, String port) {
- System.setProperty("proxySet", "true");
- System.setProperty("proxyHost", host);
- System.setProperty("proxyPort", port);
- }
然后运行代码
fidder就能监听到 请求了
对比一下 或者构造成跟 浏览器访问的一模一样 就绝对ok了
其实只要找到其中几个关键的 参数设置成一样的也是可以的
本文详细解析了在使用HttpURLConnection时遇到getContentLength()返回-1的问题,并提供了多种解决方案,包括设置请求属性以避免服务器返回gzip压缩内容,以及如何通过Fiddler工具进行深入分析。
1万+

被折叠的 条评论
为什么被折叠?



