Java Android:URLConnection.getContentLength()提供的值不正确

Java Android:URLConnection.getContentLength()提供的值不正确,java,android,android-asynctask,urlconnection,http-content-length,Java,Android,Android Asynctask,Urlconnection,Http Content Length,我正在尝试使用以下方法获取URL内容的大小: URLConnection conn = null; URL url = null; url = new URL("https://dl.dropboxusercontent.com/u/90210205/Default.html"); conn = url.openConnection(); conn.connect(); InputStream in = new BufferedInputStream(url.openStream()); int

我正在尝试使用以下方法获取URL内容的大小:

URLConnection conn = null;
URL url = null;
url = new URL("https://dl.dropboxusercontent.com/u/90210205/Default.html");
conn = url.openConnection();
conn.connect();
InputStream in = new BufferedInputStream(url.openStream());
int fileSize = conn.getContentLength();
System.out.println("File size " + fileSize);
while (in.read() != -1) {
    i++;
}
in.close();
System.out.println("Read " + i + " bytes of a possible " + fileSize);
我在
AsyncTask
doInBackground()
中使用它,出于某种原因
conn.getContentLength()正在返回
1273
,而它应该返回
10136

我已经在常规Java应用程序和
conn.getContentLength()的Main中使用了上述代码返回正确的10136值


为什么它不能在
异步任务中工作?

.getContentLength()
的描述是,它返回
内容长度
标题字段的值。在一般情况下,这应与可读取的数据量相匹配。然而

(1) 如果使用分块编码发送数据,则标头可能不存在。在本例中,
.getContentLength()
返回
-1
,因此这里的情况似乎不是这样

(2) 默认情况下,
HttpURLConnection
的实现请求服务器使用gzip压缩,并自动为
getInputStream()
的调用者解压缩数据。在这种情况下,将清除内容编码和内容长度响应头

最后一句话似乎表明这里也不是这样,因为您没有从.getContentLength()获得
-1
,但相对大小(1273/10136)让我怀疑

这方面的一个测试是通过在请求头中设置以下可接受的编码来禁用Gzip编码:

urlConnection.setRequestProperty("Accept-Encoding", "identity");
另一种方法是“窥探”网络流量,以观察http头和数据量以寻找线索


顺便说一句,
url.openStream()
对于
url.openConnection().getInputStream()

您是否使用服务器端的分块传输编码?