关于 android / ios SDK 中URLConnection 的Response 中 content length 返回-1

想做下载进度的指示条确发现服务器返回头中没有Content-Length, Android和iPhone的URLConnection都返回-1,在so上找到了这篇文章

http://stackoverflow.com/questions/7417610/why-response-expectedcontentlength-always-return-1

 

The expected content length is only set when the server provides it, such as by a Content-Length response header. A -1 size means the expected content size is unknown.

If you set Accept-Encoding: gzip on your request, the URL loading system will fib and tell you the expected size is -1, no matter what Content-Length the server sends. This is because it decompresses the data before passing it to you, but it can’t know the final uncompressed size till all the data has been downloaded, which is well after you receive this callback.

将Content-Encoding中的gzip去掉后从服务器上返的apk文件大小就可以显示了

发表评论

电子邮件地址不会被公开。 必填项已用*标注