A quick intro to HttpClient

The following is a techtip I wrote which wasn't used. Since I turned out pretty well I thought I'd post it here. Let me know what you think. Would you like more of these small self-contained tips?

A Quick Introduction to HttpClient

Java is great because it has classes for almost everything. For example, if you want to open a webpage you can do it with the java.net.URL class. But what if you want to use a POST instead of a GET request? What if you have a bunch of parameters that need to be properly parsed? What if you want to deal with cookies? The URL class simply isn't up to the task. However, in Java, you are always just a single jar away from the solution. In this case, you can use the HTTP Client library from the Apache commons project here. You will need to put the commons-httpclient.jar and commons-logging.jar files in your path.

To use the HTTP Client import the org.apache.commons.httpclient package and get started:

This is how to do a simple POST request

import org.apache.commons.httpclient.*;
...
// initialize the POST method
PostMethod post = new PostMethod("http://myserver/page.jsp");
post.addParameter("parameter1", "value1");
post.addParameter("parameter2", "value2");

// execute the POST
HttpClient client = new HttpClient();
int status = client.executeMethod(post); 
String response = post.getResponseBodyAsString();

You can set up all of your parameters using the PostMethod class and then execute the POST with the executeMethod() on the post object. After the post has completed you can read the body of the response into a string or look at the HTTP status code. The classic 404 error is an example of an HTTP status code. The example above posted with a set of parameters. If instead you want to just post an entire document, say an XML document or SOAP request, then you could do something like this:

StringBuffer content = new StringBuffer();
content.append("");
content.append("information");
content.append("");
post.setRequestBody(content.toString());
client.executeMethod(post);

Now lets say you need to list the cookies set by a website. That's also very easy to do. The HttpClient object acts as a miniature browser. It will preserve all state about the current HTTP session, including cookies, in an HttpState object. After you have connected to the website through a get or post you can look at the cookies set by the webserver like this:

        int status = client.executeMethod(get);

        HttpState state = client.getState();
        for(Cookie c : state.getCookies()) {
            System.out.println("cookie = " + c.getName() + "=" + c.getValue());
        }

Since HttpClient maintains state about the HTTP session you can also use it to log into secure sites. For example, if I wanted to show the file listing my secure webdav server, the same way a real browser would see it, I could log in like this:

	// create credentials to log in
        Credentials cred = new UsernamePasswordCredentials("myusername","mypassword");
	// set the realm to null, meaning use these credentials for all websites
        client.getState().setCredentials(null,cred);

	// get the html doc
        GetMethod get = new GetMethod("http://files.myserver.com/secretprojects/");
        int ret = client.executeMethod(get);
        System.out.printlin("a browser sees = " + get.getResponseBodyAsString());

The HttpClient library is very powerful, enabling you to perform almost any HTTP task such as GETs, POSTs, getting and setting cookies, handling redirects, going through HTTP proxies, and even HTTPS authentication. And best of all, it is freely available under Apache License. You can download the HttpClient library at

http://jakarta.apache.org/commons/httpclient/

Talk to me about it on Twitter

Posted November 1st, 2006

Tagged: java.net