Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable default request headers from apache httpclient 4?

I am using apache common httpclient 4.3.3 to make http 1.0 request. Here is how I make the request

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
post.setProtocolVersion(new ProtocolVersion("HTTP", 1, 0));

 // trying to remove default headers but it doesn't work
post.removeHeaders("User-Agent");
post.removeHeaders("Accept-Encoding");
post.removeHeaders("Connection");

post.setEntity(new ByteArrayEntity(ba) );

HttpResponse response = client.execute(post);

However, i can see that there are other headers automatically added to my request to the server like

Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.3.3 (java 1.5)
Accept-Encoding: gzip,deflate

How can I tell httpclient not to include any other headers? I tried to removed those headers with post.removeHeaders(xxxx) but it doesn't work. Can you show me how?

Thanks,

like image 316
Sean Nguyen Avatar asked Apr 25 '14 11:04

Sean Nguyen


People also ask

Do we need to close HttpClient?

You do not need to explicitly close the HttpClient, however, (you may be doing this already but worth noting) you should ensure that connections are released after method execution. Edit: The ClientConnectionManager within the HttpClient is going to be responsible for maintaining the state of connections.

How do you add a header to an HTTP request in Java?

Instead of setting the Header on each and every request, we can also configure it as a default header on the Client itself: Header header = new BasicHeader(HttpHeaders. CONTENT_TYPE, "application/json"); List<Header> headers = Lists. newArrayList(header); HttpClient client = HttpClients.

Is Apache HttpClient deprecated?

From Apache HTTP Client API version 4.3 on wards, DefaultHttpClient is deprecated.


3 Answers

If you call HttpClientBuilder.create(), you will have a httpClientBuilder. And httpClientBuilder has a lot config for default headers and this will be used to make intercepters( ex: RequestAcceptEncoding ).

For example, RequestAcceptEncoding, which implements HttpRequestInterceptor, makes Accept-Encoding: gzip,deflate header when HttpProcessor.process() is invoked. And httpProcessor.process() will be invoked just before invoking final CloseableHttpResponse response = this.requestExecutor.execute(route, request, context, execAware);

You can see this code at org.apache.http.impl.execchain.ProtocolExec of httpclient-4.3.6 line 193.

If you want to remove Accept-Encoding: gzip,deflate, call HttpClientBuilder.disableContentCompression() like below.

HttpClient client = HttpClientBuilder.create().disableContentCompression().build();

In short, HttpClientBuilder has a lot of flags to disable/enable HttpRequestInterceptor. If you disable/enable those HttpRequestInterceptor, you can exclude/include default headers.

Sorry for my poor English, and hope you get what I mean.

like image 67
Cheemin Avatar answered Oct 21 '22 05:10

Cheemin


CloseableHttpClient hc = HttpClients.custom()
        .setHttpProcessor(HttpProcessorBuilder.create().build())
        .build();

The code snippet above demonstrates how to create an HttpClient instance with an empty (no-op) protocol processor, which guarantees no request headers will ever be added to outgoing messages executed by such client

like image 38
ok2c Avatar answered Oct 21 '22 03:10

ok2c


You want to do your 'cleanup' at the end, after HttpClient is done modifying the request. You can do this by calling addInterceptorLast on HttpClientBuilder as below.

HttpClient client = HttpClientBuilder.create().addInterceptorLast( 
                        new HttpRequestInterceptor() {
                            public void process(HttpRequest request, HttpContext context){
                                request.removeHeaders("Host");
                                request.removeHeaders("Connection");
                            }
                        }   
                ).build();

I create an anonymous class implementing HttpRequestInterceptor. Take whatever header modifications you need done before the request is sent, and put them in the process method.

like image 25
Brian G Avatar answered Oct 21 '22 04:10

Brian G