默认情况下,HttpURLConnection使用gzip方式获取,要取得长度则要求http请求不要gzip压缩;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept-Encoding", "identity"); // 添加这行代码
conn.connect();
int length = conn.getContentLength();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept-Encoding", "identity"); // 添加这行代码
conn.connect();
int length = conn.getContentLength();
禁用HTTP gzip压缩
本文介绍如何通过设置HTTP请求头来禁用HttpURLConnection的gzip压缩,以准确获取响应内容的长度。

被折叠的 条评论
为什么被折叠?



