Apache Commons HTTP Components HTTPClient

Did your code using Apache Commons HTTPClient just throwed the org.apache.http.HttpException: Unsupported Content-Coding: none exception? No worries, I have a solution.

Reason of the exception: org.apache.http.HttpException: Unsupported Content-Coding: none

The org.apache.http.HttpException: Unsupported Content-Coding: none exception is being thrown when you communicate with a server that sets standards-incompatible Content-Encoding: none header.

Content-Encoding header can take only five values: gzip, compress, deflate, identity, br. The “none” is therefore an invalid value. The valid value to indicate that content is not being encoded is identity. One of the solutions is to fix the server you are connecting to to use a proper value. But because often we work with third-party servers, I will present a solution that is done completely on the client side.

Solution explanation for org.apache.http.HttpException: Unsupported Content-Coding: none

My solution is 100% client-side solution. That means, we don’t have to interfere with remote servers in any way. We fix everything on the Java-side.

The org.apache.http.HttpException: Unsupported Content-Coding: none is being caused by a mechanism that detects Content-Encoding of response. That mechanism automatically decompresses any compresses responses. Unfortunately for users connecting to badly configured servers, this mechanism will also throw org.apache.http.HttpException: Unsupported Content-Coding: none if Content-Encoding contains any non-standard value.

One of possible solutions for this problem is to replace this badly set Content-Encoding header with a valid one before it gets processed by the earlier mentioned mechanism. To do this, we will utilize HttpResponseInterceptor together with a custom HttpProcessor.

HttpResponseInterceptor is a utility that allows to “intercept” a response before it gets returned. HttpProcessor is a mechanism that is responsible for processing of HTTP requests and responses.

We will utilize HttpResponseInterceptor inside HttpProcessor before everything else. This way, the badly set Content-Encoding header will get replaced with a valid one before triggering the mechanism that throws org.apache.http.HttpException: Unsupported Content-Coding: none when it finds non-standard value.

Solution code for org.apache.http.HttpException: Unsupported Content-Coding: none

First let’s start with creating a HttpResponseInterceptor that will fix bad Content-Encoding header:

HttpResponseInterceptor contentEncodingFixerInterceptor = new HttpResponseInterceptor()  {
    @Override
    public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
        Header contentEncodingHeader = response.getFirstHeader(HTTP.CONTENT_ENCODING);
        if(contentEncodingHeader != null && contentEncodingHeader.getValue().equalsIgnoreCase("none")) {
            response.removeHeaders(HTTP.CONTENT_ENCODING);
            response.addHeader(HTTP.CONTENT_ENCODING, "identity");
        }
    }
};

Then let’s create our own custom HttpProcessor that will fire up our interceptor before anything else:

BasicHttpProcessor httpProcessor = new BasicHttpProcessor();

httpProcessor.addInterceptor(contentEncodingFixerInterceptor);

// Required protocol interceptors
httpProcessor.addInterceptor(new RequestContent());
httpProcessor.addInterceptor(new RequestTargetHost());
httpProcessor.addInterceptor(new RequestDefaultHeaders());

// Recommended protocol interceptors
httpProcessor.addInterceptor(new RequestConnControl());
httpProcessor.addInterceptor(new RequestUserAgent());
httpProcessor.addInterceptor(new RequestExpectContinue());

// HTTP state management interceptors
httpProcessor.addInterceptor(new RequestAddCookies());
httpProcessor.addInterceptor(new ResponseProcessCookies());

// HTTP authentication interceptors
httpProcessor.addInterceptor(new RequestTargetAuthentication());
httpProcessor.addInterceptor(new RequestProxyAuthentication());

And finally, let’s apply our HttpProcessor to our HttpClient:

CloseableHttpClient httpClient = HttpClients.custom().setHttpProcessor(httpProcessor).build();

And that’s all! Don’t forget to add required imports. Your IDE should do that for you.

This is the solution to org.apache.http.HttpException: Unsupported Content-Coding: none exception. Let me know if it worked for you!