hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: adding interceptors on per request basis (was Re: Unable to see response encoding even through "Content-Encoding" header is present)
Date Fri, 10 Jan 2014 10:03:15 GMT
On Thu, 2014-01-09 at 13:34 -0800, Gaurav Kumar wrote:
> In my use case, I need to know on per request basis what was the encoding
> used by remote server. I was hoping there there was out of the box way to
> know content-encoding but I am okay with disabling decompression. Thanks
> for the suggestion.
> 
> 

You do not have to disable it. You can still use a custom protocol
interceptor to get the job done.
 
---
CloseableHttpClient httpclient = HttpClients.custom()
        .addInterceptorFirst(new HttpResponseInterceptor() {
            @Override
            public void process(
                    HttpResponse response,
                    HttpContext context) throws HttpException,
IOException {
                HttpEntity entity = response.getEntity();
                if (entity != null && entity.getContentLength() != 0) {
                    Header ceheader = entity.getContentEncoding();
                    if (ceheader != null) {
                        context.setAttribute("mystuff",
ceheader.getValue());
                    }
                }
            }
        }).build();
HttpClientContext context = HttpClientContext.create();
CloseableHttpResponse response = httpclient.execute(new
HttpGet("/stuff"), context);
try {
    String originalEncoding = context.getAttribute("mystuff",
String.class);
} finally {
    response.close();
}
---

Oleg



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message