Extracting gzip data from Apache-httpclient without decompression

房东的猫 提交于 2020-05-15 02:14:24

问题


I'm using apache httpclient 4.3.5 to send a request to an upstream server which returns a gzipped response. I need to pass this response AS-IS to a downstream server without any form of decompression. However, httpclient is far too helpful and insists on decompressing the response and I can't find any way of persuading it to stop.

CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse serverResponse = client.execute(serverRequest);
try {
    HttpEntity entity = serverResponse.getEntity();
    downstreamResponse.setStatus(serverResponse.getStatusLine().getStatusCode());
    for (Header header : serverResponse.getAllHeaders()) {
        downstreamResponse.setHeader(header.getName(), header.getValue());
    }
    entity.writeTo(downstreamResponse.getOutputStream());
    downstreamResponse.flushBuffer();
} finally {
    serverResponse.close();
}

I'm sure that there is some way of configuring the client using some form of the construct

return HttpClients.custom()
    ....
    .build();

but I can't find it. Can the experts please advise?


回答1:


CloseableHttpClient client = HttpClients.custom()
        .disableContentCompression()
        .build();


来源:https://stackoverflow.com/questions/30216375/extracting-gzip-data-from-apache-httpclient-without-decompression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!