OkHttp 3:如何在Java/Android中手动解压Gzip/Deflate响应

10
我知道okhttp3库默认添加头部Accept-Encoding: gzip并自动为我们解码响应。然而,我遇到了一个只接受Accept-Encoding: gzip, deflate头部的主机。如果我不加上deflate部分,则会失败。现在当我手动将该头部添加到okhttp客户端时,该库不再为我执行解压缩操作。
我尝试了多种解决方案来手动解压缩响应,但最终都出现了异常,例如java.util.zip.ZipException: Not in GZIP format。以下是我迄今为止尝试过的方法:
//decompresser
public static String decompressGZIP(InputStream inputStream) throws IOException
{
    InputStream bodyStream = new GZIPInputStream(inputStream);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[4096];
    int length;
    while ((length = bodyStream.read(buffer)) > 0) 
    {
        outStream.write(buffer, 0, length);
    }

    return new String(outStream.toByteArray());
}


//run scraper
scrape(api, new Callback()
{
    // Something went wrong
    @Override
    public void onFailure(@NonNull Call call, @NonNull IOException e)
    {
    }

    @Override
    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException
    {
        if (response.isSuccessful())
        {
            try
            {
                InputStream responseBodyBytes = responseBody.byteStream();
                returnedObject = GZIPCompression.decompress(responseBodyBytes);

                if (returnedObject != null)
                {
                    String htmlResponse = returnedObject.toString();
                }
            }
            catch (ProtocolException e){}

            if(response != null) response.close();
        }
    }
});



private Call scrape(Map<?, ?> api, Callback callback)
{
    MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    String method = (String) api.get("method");
    String url = (String) api.get("url");
    Request.Builder requestBuilder = new Request.Builder().url(url);
    RequestBody requestBody;

    requestBuilder.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0");
    requestBuilder.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    requestBuilder.header("Accept-Language", "en-US,en;q=0.5");
    requestBuilder.header("Accept-Encoding", "gzip, deflate");
    requestBuilder.header("Connection", "keep-alive");
    requestBuilder.header("Upgrade-Insecure-Requests", "1");
    requestBuilder.header("Cache-Control", "max-age=0");

    Request request = requestBuilder.build();

    Call call = client.newCall(request);
    call.enqueue(callback);

    return call;
}

请注意,响应头将始终返回Content-Encoding: gzipTransfer-Encoding: chunked

还有一件事,我也尝试过这个主题中的解决方案,但仍然出现D/OkHttp: java.io.IOException: ID1ID2: actual 0x00003c68 != expected 0x00001f8b

任何帮助都将不胜感激。

3个回答

24

挖掘了6个小时后,我找到了正确的解决方案,就像往常一样,比我想象中要简单,所以我基本上是在尝试解压一个未经gzip压缩的页面,因此它失败了。现在,一旦我进入第二页(压缩的页面),我会得到一个gzip响应,代码应该处理它。如果有人需要解决方案,我使用了一个修改过的拦截器,就像这个答案中所示,因此您不需要使用自定义函数来处理解压缩。

我修改了unzip方法,使okhttp的interceptor可以处理压缩和未压缩的响应:

OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().addInterceptor(new UnzippingInterceptor());
OkHttpClient client = clientBuilder.build();

拦截器的作用就像这样:

private class UnzippingInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        return unzip(response);
    }
  

// copied from okhttp3.internal.http.HttpEngine (because is private)
private Response unzip(final Response response) throws IOException {
    if (response.body() == null)
    {
        return response;
    }
    
    //check if we have gzip response
    String contentEncoding = response.headers().get("Content-Encoding");
    
    //this is used to decompress gzipped responses
    if (contentEncoding != null && contentEncoding.equals("gzip"))
    {
        Long contentLength = response.body().contentLength();
        GzipSource responseBody = new GzipSource(response.body().source());
        Headers strippedHeaders = response.headers().newBuilder().build();
        return response.newBuilder().headers(strippedHeaders)
                .body(new RealResponseBody(response.body().contentType().toString(), contentLength, Okio.buffer(responseBody)))
                .build();
    }
    else
    {
        return response;
    }
}
}

我讨厌人们不包含导入语句的时候。 - undefined

0

因为 okhttp 不支持 deflate

在 BridgeInterceptor.java 或 BridgeInterceptor.kt 中

    if (transparentGzip &&
    "gzip".equals(networkResponse.header("Content-Encoding"), ignoreCase = true) &&
    networkResponse.promisesBody()) {

0

如果您的标头包含gzip,版本4.10.0已经可以自动完成此操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接