Pierre Gayvallet
Pierre Gayvallet

Reputation: 2953

Problem displaying request headers with apache httpclient 4

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 ?

Upvotes: 3

Views: 8064

Answers (3)

CodeChiller
CodeChiller

Reputation: 121

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());
}

Upvotes: 12

4pins
4pins

Reputation: 41

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.

Upvotes: 4

BalusC
BalusC

Reputation: 1109865

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.

Upvotes: 2

Related Questions