Skip to main content

A quick intro to HttpClient

Posted by joshy on November 1, 2006 at 12:50 AM PST

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/

Related Topics >>

Comments

Hi Josh, Nice post! Can you

Hi Josh,
Nice post! Can you give me a help!? Please!?
I'm trying to use these examples but when trying to run the app I'm receiving this... I know I will need to do something with the certificate, but what?

20/10/2010 12:09:07 org.apache.commons.httpclient.HttpMethodBase readResponseBody
INFO: Response content length is not known
Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at org.apache.commons.httpclient.HttpConnection.flushRequestOutputStream(HttpConnection.java:828)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2116)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at Main.main(Main.java:28)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 18 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 24 more