Home > Java > javaTutorial > body text

Some ideas on why getContentLength() is always -1 in JAVA download

巴扎黑
Release: 2017-06-26 10:21:24
Original
1815 people have browsed it
If Content Length is not described in the header file

# #There is no solution yet

If Content Long is described in the header file

Option 1:

Disguise as a browser

 conn.setRequestProperty("User-Agent", " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
Copy after login
Add proxy to the code

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);    
}
Copy after login

Option 2:

Add the following attributes to prevent the server from gzip compression:

Java Doc has a description of this:

By default, this implementation of HttpURLConnection requests that servers use gzip compression. Since getContentLength() returns the number of bytes transmitted, you cannot use that method to predict how many bytes can be read from getInputStream(). Instead, read that stream until it is exhausted: whenread() returns - 1.

conn.setRequestProperty("Accept-Encoding", "identity");
Copy after login

The above is the detailed content of Some ideas on why getContentLength() is always -1 in JAVA download. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!