Wednesday, 22 June 2011

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;
}
}
}


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.



2 comments:

  1. Thanks - your proxy setting code helped me a lot. I was able to connect to local urls but external urls were firewalled.

    I tried to change it in eclipse.ini - it did not help. I tried to change in debug config - it did not help. Spent the whole day trying to resolve why a simple junit call would not pick any of these values and then i found your code via google!!!

    thanks a bunch!

    ReplyDelete
  2. My pleasure, if it did helped you :)

    ReplyDelete