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