我知道该okhttp3库默认情况下会添加标头Accept-Encoding: gzip并自动为我们解码响应。我正在处理的问题是一个只接受标头的主机,例如:Accept-Encoding: gzip, deflate如果我不添加该deflate部分,它就会失败。现在,当我手动将该标头添加到 okhttp 客户端时,该库不再为我进行解压缩。我尝试了多种解决方案来获取响应并尝试手动解压缩它,但我总是以异常结束,即java.util.zip.ZipException: Not in GZIP format,这是我迄今为止尝试过的://decompresserpublic 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 scraperscrape(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);请注意,响应标头将始终返回Content-Encoding: gzip并且Transfer-Encoding: chunked还有一件事,我也尝试过本主题中的解决方案,但它仍然以D/OkHttp: java.io.IOException: ID1ID2: actual 0x00003c68 != expected 0x00001f8b.任何帮助,将不胜感激..
添加回答
举报
0/150
提交
取消