HttpClient and Web Sniffers

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.
Credits for 3rd solution- http://www.reddit.com/user/onebit
Advertisements

About Gaurav

I'm Gaurav; friends call me Teddy, n i'm shivi fr my family!! A java enthusiast who likes experimenting with what he knows. View all posts by Gaurav

4 responses to “HttpClient and Web Sniffers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: