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 ?
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.