本文介绍了HttpURLConnection不解压缩Gzip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Gingerbread + Android设备上使用HttpURLConnection,但gzip编码遇到问题.根据文档

I'm trying to use HttpURLConnection on Gingerbread+ Android devices and am having trouble with the gzip encoding. According to the documentation

接受编码:gzip"

Accept-Encoding: gzip"

问题是这实际上没有发生.完全不会添加Accept-Encoding:gzip标头.如果我手动添加它,那么我希望它的解压缩部分可以通过connection.openInputStream()来自动返回GZipInputStream,但它仅返回一个普通的InputStream.

The problem is that this is not actually happening. The Accept-Encoding: gzip header is not getting added at all. If I add it manually, I would then expect the decompressing part of it to work by connection.openInputStream() to automatically return a GZipInputStream but it just returns a plain InputStream.

有人经历过吗?我还没有看到发生这种情况的任何帖子,所以非常奇怪.该项目是根据API 17编译的,因此应该不会出现问题,并且设备正在运行4.3.

Has anyone experienced this? I havent seen any posts of this happening so its very odd. The project is compiled against API 17 so that shouldnt be a problem and the device is running 4.3.

谢谢.

推荐答案

我遇到了同样的问题,并且与 HTTPS 有关.如果您致电:

I had the same problem and it was related to HTTPS. If you call:

URL url = new URL("https://www.example.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

您实际上获得了一个不会自动处理gzip的HttpsURLConnection(HttpURLConnection的子类)的实例.在这种情况下,您可以:

you actually get an instance of HttpsURLConnection (a subclass of HttpURLConnection) that does NOT handle gzip automatically.In those cases you can:

conn.setRequestProperty("Accept-Encoding", "gzip");
...
InputStream inStream = new GZIPInputStream(conn.getInputStream());

这篇关于HttpURLConnection不解压缩Gzip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:50
查看更多