3

I've been trying to retrieve the headers sent by a HttpMethod, using HttpClient 4, but without any success...

here is my code :

HttpClient httpClient = new DefaultHttpClient();
HttpParams httpParams = httpClient.getParams();
HttpGet httpGet = new HttpGet("http://www.google.fr");

HttpResponse response = httpClient.execute(httpGet);

log.info("*** Request headers ***");
Header[] requestHeaders = httpGet.getAllHeaders();
for(Header header : requestHeaders) {
    log.info(header.toString());
}
log.info("***********************");


log.info("*** reponse ***");
log.info(response.getStatusLine());
Header[] headers = response.getAllHeaders();
for(Header header : headers) {
    log.info(header.toString());
}

but the result is :

00:27:57,368 INFO   - *** Request headers ***

00:27:57,368 INFO   - ***********************

00:27:57,368 INFO   - *** reponse ***

00:27:57,368 INFO   - HTTP/1.1 200 OK

00:27:57,368 INFO   - Date: Sun, 15 Aug 2010 22:28:09 GMT

00:27:57,368 INFO   - Expires: -1

00:27:57,368 INFO   - Cache-Control: private, max-age=0

00:27:57,368 INFO   - Content-Type: text/html; charset=ISO-8859-1

00:27:57,368 INFO   - Set-Cookie: 

[..]

Aka the response headers are good, but not the request's. ( Same result if I move the log request headers block before the execute statement ).

(and NO, I dont want to simply see them, so setting the log level to debug isnt acceptable )

Anyone can help ?

3 Answers 3

12

To get all headers including those which the HTTPclient sets, use
HttpCoreContext. This class allows to read out all the headers.

HttpClient client = HttpClients.createDefault();
HttpCoreContext localContext = new HttpCoreContext();
HttpResponse response = client.execute(request,localContext);

Header[] headers = localContext.getRequest().getAllHeaders();
for (Header header : headers) {
   System.out.println(header.toString());
}
Sign up to request clarification or add additional context in comments.

Comments

4

Things may have changed since 2010, however this can be done by using a request interceptor (to inspect the request at a lower level) with amazingly similar code.

// So we can get all the headers (not just the ones we explicitly set).        
httpClient.addRequestInterceptor(new HttpRequestInterceptor() {

    public void process(
            final HttpRequest request,
            final HttpContext context) 
            throws HttpException, IOException {

        // Start Debug
        System.out.println("*** Request headers ***");
        Header[] requestHeaders = request.getAllHeaders();
        for(Header header : requestHeaders) {
            System.out.println(header.toString());
        }
        System.out.println("***********************");
        // End Debug
    }

});

In my case, I get the following output (having only explicitly set two of these).

*** Request headers ***
Accept: application/xml
Authorization: Basic bmV3Omd1ZXN0
Content-Length: 772
Content-Type: application/xml; charset=UTF-8
Host: rest3api.sifassociation.org:80
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.2.1 (java 1.5)
***********************

May this help those who travel here.

Comments

2

It will only display the request headers you've set yourself.

If you want to log the request headers which HttpClient has set, then you need to configure HttpClient's builtin logging by Commons Logging. Also see this document.

As an alternative, you can also use an external tool like Fiddler.

2 Comments

Thanks for the answer but I said changing the log level isnt acceptable : I need to store the sent headers. You sure there is no way to get them, before or even after executing the request ?
While this answer is perfect okay, I think solving the problem without logging is a better solution (see the most upvoted answer). <sub>(I didn't vote this answer.)</sub>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.