解决方法:
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();
}
参考链接:点击打开链接
获取网络大小使用的代码如下:
正常情况下能获取到 文件的大小
如果返回 -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 伪装一下 伪装成浏览器 后就可以了
如果还不能解决 那就得用fidder工具好好分析一下了
通过fidder分析 我们发出的请求头 request跟浏览器发出的 request 有哪些区别
浏览器的request很容易获取
打开 fidder,fidder会自动获取到浏览器中输入的链接的 相关信息。
而我们代码 运行的request则需要在代码中 加 代理
指向本机的 fidder监听端口
工具----> fidder选项--->监听端口 可以查看端口
代码中加代理
然后运行代码
fidder就能监听到 请求了
对比一下 或者构造成跟 浏览器访问的一模一样 就绝对ok了
其实只要找到其中几个关键的 参数设置成一样的也是可以的