Introduction

When I say HttpClient, I talk about a package i.e. org.apache.commons.httpclient in commons-httpclient. HttpClient is used for making post and get request to particular URIs. And one can all kinds of things with the response and response headers.

A simple example of sending a:

HttpClient client = new HttpClient(); HttpMethod method = new GetMethod("http://localhost.:8080/web-service/"); try { client.executeMethod(method); System.out.println("Method executed."); // check if the request returned the desired http response code if (method.getStatusCode() == HttpStatus.SC_OK) { // fetch response String response = method.getResponseBodyAsString(); System.out.println("Response = " + response); } else { System.out.println("Failed " + method.getStatusCode()); } } catch (IOException e) { e.printStackTrace(); } finally { method.releaseConnection(); }

What’s strange with HttpClient?

By default, a http request sent using HttpClient is not captured by the web sniffers e.g. fiddler. I have no clue why that happens. But that is a pain in the neck. If one wants to test the request headers and response headers for this request, one simply fails to capture the packets.

Solutions

1. Adjusting code to send request through proxy

Well, no matter how strange a problem is, there always is a solution hiding beyond the obvious. Fiddler uses a proxy client and port. If you somehow send your request via the fiddler proxy, it starts capturing your packets.

Add the following highlights lines in the code, and the problem is fixed magically:

final String PROXY_HOST = "localhost"; final int PROXY_PORT = 8888; HttpClient client = new HttpClient(); HttpMethod method = new GetMethod("http://localhost.:8080/web-service-0.0.1-SNAPSHOT/"); try { HostConfiguration config = client.getHostConfiguration(); config.setProxy(PROXY_HOST, PROXY_PORT); client.executeMethod(method); System.out.println("Method executed."); // check if the request returned the desired http response code if (method.getStatusCode() == HttpStatus.SC_OK) { // fetch response String response = method.getResponseBodyAsString(); System.out.println("Response = " + response); } else { System.out.println("Failed " + method.getStatusCode()); } } catch (IOException e) { e.printStackTrace(); } finally { method.releaseConnection(); }

2. Run jvm with proxy settings instead of making changes to the code -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8888</div> 3. Turning on logging is another solution to the problem Turning on the finer logs may reveal a lot about the request and response headers. Please have a look at http://hc.apache.org/httpclient-3.x/logging.html Credits for 3rd solution- http://www.reddit.com/user/onebit