且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HttpURLConnection不解压缩Gzip

更新时间:2023-11-10 08:22:46

我遇到了同样的问题,并且与 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());