HttpClient (Apache commons) code sample

I was playing around with Apache commons Http utilities the last day. I used to use the java.net.* APIs to satisfy my HTTP(s) needs.

Here is a sample code which I wrote which takes a URL as input, sets the basic request parameters (e.g. cookie string) and set the proxy settings along with the user credentials.

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpClientTest
{
    public static void main(String[] args)
    {
        HttpClient client = null;
        GetMethod getMethod = null;
        int responseCode = -1;
        byte[] responseStream = null;

        String urlString = "http://www.facebook.com";
        String cookieString = null;

        try
        {
            // Creating the GetMethod instance
            getMethod = new GetMethod(urlString);

            // Retries to establish a successful connection the specified number
            // of times if the initial attempts are not successful.
            getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                    new DefaultHttpMethodRetryHandler(1, false));
            getMethod.getParams().setParameter("http.socket.timeout", new Integer(5000));
            getMethod.setRequestHeader(new Header("Cookie", "<COOKIE_STRING>"));

            // Creating an HttpClient instance
            client = new HttpClient();

            // Proxy settings: Configures the proxy host, port & user
            // credentials and the scope of the credentials.
            client.getHostConfiguration().setProxy("<HOST>", <PORT>);
            Credentials credentials = new UsernamePasswordCredentials
                ("<USERNAME>", "<PASSWORD>");
            AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
            client.getState().setProxyCredentials(scope, credentials);

            // Sets the user-agent for the client instance
            client.getParams().setParameter("http.useragent", "<USER_AGENT>");

            // Sends the GET request and gets the response
            responseCode = client.executeMethod(getMethod);
            responseStream = getMethod.getResponseBody();

            System.out.println("Response Code: " + responseCode);
            System.out.println("Response Body: \n" + new String(responseStream));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            getMethod.releaseConnection();
            client = null;
        }
    }
}

P.S. Did I mention that this is test code? 🙂

Note: I was looking for an API set which does make use of its own Socket level implementation. I don’t think HttpClient got it’s own implementation as it claims to be 100% Java. If you know any API set which performs better than java.net.* APIs please feel free to share it.

Advertisements