HttpURLConnection getContentLength返回-1

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

 

 

全部源码:

 

 

 
  1. try {

  2. //打开链接

  3. URL url = new URL(strings[0]);

  4. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

  5.  
  6. //必须加上下面三句,要不total为-1

  7. urlConnection.setRequestMethod("POST");

  8. urlConnection.setRequestProperty("Accept-Encoding", "identity");

  9. 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");

  10.  
  11. if (200 == urlConnection.getResponseCode()) {

  12. Log.i(TAG, "200");

  13. //得到输入流

  14. InputStream is = urlConnection.getInputStream();

  15. ByteArrayOutputStream baos = new ByteArrayOutputStream();

  16. byte[] buffer = new byte[1024];

  17. int len = 0;

  18. int count = 0;

  19.  
  20. long total = urlConnection.getContentLength();

  21. Log.i(TAG, "total = " + total);

  22. while (-1 != (len = is.read(buffer))) {

  23. baos.write(buffer, 0, len);

  24. count += len;

  25. publishProgress((int) ((count / (float) total) * 100));

  26. Log.i(TAG, " baos = " + baos.toString("utf-8"));

  27. Thread.sleep(500);

  28. }

  29. Log.i(TAG, " baos = " + baos.toString("utf-8"));

  30. return baos.toString("utf-8");

  31. } else {

  32. Log.i(TAG, " 系统错误 code = " + urlConnection.getResponseCode());

  33. }

  34. } catch (MalformedURLException e) {

  35. Log.i(TAG, "URLException = " + e.toString());

  36. e.printStackTrace();

  37. } catch (IOException e) {

  38. Log.i(TAG, "IOException = " + e.toString());

  39. e.printStackTrace();

  40. } catch (InterruptedException e) {

  41. Log.i(TAG, "InterruptedException = " + e.toString());

  42. e.printStackTrace();

  43. }

 

 

 

参考链接:点击打开链接

 

获取网络大小使用的代码如下:

 

[java] view plain copy

  1. URL url=new   URL("http://yinyueshiting.baidu.com/data2/music/123296359/13132725248400128.mp3?xcode=bce09e68531be7afafd0ef4fac9a741419c56da7f7cc7a9f");     
  2. HttpURLConnection   urlcon=(HttpURLConnection)url.openConnection();     
  3. //根据响应获取文件大小   
  4. int   fileLength=urlcon.getContentLength(); //这里获取的是字节    
  5.  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的大小

 

则把该链接在浏览器的下载任务中新建 尝试下载

HttpURLConnection getContentLength返回-1

HttpURLConnection getContentLength返回-1

 

 

 

如果能下载,则说明链接正确,服务器也没问题,ip也没被屏蔽也就是我们的代码有问题。

 

网上一些解决方案说是HttpURLConnection 使用 gzip方式获取 的原因。

加上代码

 

urlcon.setRequestProperty("Accept-Encoding", "identity"); 

可以解决

 

详见

Android getContentLength()为-1 解决方法

 

 

如果上述方法仍然不行

 

那继续往下分析

我们在谷歌浏览器中访问这个链接

查看它的请求头以及response 发现response中是有content-length的 一般都会有

HttpURLConnection getContentLength返回-1

 

那我们的代码问题到底出在哪呢

 

一般可能是请求头的问题  导致被服务器拒绝访问了

 

我的就是这个原因

 

加上User-agent 伪装一下 伪装成浏览器 后就可以了

 

[java] view plain copy

  1. URL url;  
  2.                         url = new   URL();                            
  3.                         HttpURLConnection   urlcon=(HttpURLConnection)url.openConnection();     
  4.                         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");  
  5.                         urlcon.setConnectTimeout(15000);  
  6.                         urlcon.setReadTimeout(15000);  
  7.                         //根据响应获取文件大小   
  8.                         fileLength=urlcon.getContentLength();  
  9.                         System.out.println("get fileLen "+fileLength);  

 

 

如果还不能解决 那就得用fidder工具好好分析一下了

通过fidder分析 我们发出的请求头 request跟浏览器发出的 request 有哪些区别

 

浏览器的request很容易获取 

 

打开 fidder,fidder会自动获取到浏览器中输入的链接的 相关信息。

HttpURLConnection getContentLength返回-1

 

 

而我们代码 运行的request则需要在代码中 加 代理

指向本机的 fidder监听端口

工具----> fidder选项--->监听端口 可以查看端口

HttpURLConnection getContentLength返回-1

 

 

代码中加代理 

 

[java] view plain copy

  1. String host = "127.0.0.1";    
  2.      String port = "8888";    
  3.      setProxy(host, port);   
  4.      public static void setProxy(String host, String port) {    
  5.     System.setProperty("proxySet", "true");    
  6.     System.setProperty("proxyHost", host);    
  7.     System.setProperty("proxyPort", port);    
  8. }   

 

 

然后运行代码

fidder就能监听到 请求了 

 

对比一下 或者构造成跟 浏览器访问的一模一样 就绝对ok了

 

其实只要找到其中几个关键的 参数设置成一样的也是可以的