JAVA download getContentLength () has been for some ideas of-1, getcontentlength-1

Source: Internet
Author: User

JAVA download getContentLength () has been for some ideas of-1, getcontentlength-1

If Content Length is not described in the header file

No solution yet

If Content Long is described in the header file

 


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

Add a 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);    }   

 

 

Solution 2:

Add the following attributes so that the server does not require gzip compression:

Java Doc has a description of this:

By default, this implementationHttpURLConnectionRequests that servers use gzip compression. SincegetContentLength()Returns the number of bytes transmitted, you cannot use that method to predict how many bytes can be read fromgetInputStream(). Instead, read that stream until it is exhausted: whenread()Returns-1.

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.