簡體   English   中英

okhttp 3:如何使用 Java/Android 手動解壓縮 gzip/deflate 響應

[英]okhttp 3: how to decompress gzip/deflate response manually using Java/Android

我知道okhttp3庫默認添加 header Accept-Encoding: gzip並自動為我們解碼響應。

我正在處理一個只接受 header 的主機的問題,例如: Accept-Encoding: gzip, deflate if I don't add the deflate part it fails. 現在,當我手動將 header 添加到 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

任何幫助,將不勝感激..

經過 6 個小時的挖掘,我找到了正確的解決方案,而且像往常一樣,它比我想象的要容易,所以我基本上是在嘗試解壓縮一個沒有 gzip 的頁面,因為這個原因它失敗了。 現在,一旦我點擊第二頁(已壓縮),我就會得到一個 gzipped 響應,上面的代碼應該處理它。 另外,如果有人想要解決方案,我使用了一個修改過的攔截器,就像這個答案中的攔截器一樣,所以你不需要使用自定義函數來處理解壓。

我修改了unzip方法,使 okhttp interceptor可以處理壓縮和未壓縮的響應:

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

攔截器就像dis:

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;
    }
}
}

如果您的 header 包含gzip ,版本4.10.0已經可以自動完成

因為okhttp不支持deflate

在 BridgeInterceptor.java 或 BridgeInterceptor.kt

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM