ManticMoo.COM All Articles Jeff's Articles
Jeffrey P. Bigham

Setting the User-Agent string in the Jakarta Commons HttpClient

Jeffrey P. Bigham

Related Ads

The Jakarta Commons HttpClient is an incredibly useful tool put out by the Apache Jakarta Project. It basically functions as a self-contained little web browser with a programmatic interface. One thing that you might need to do to get websites to respond to you like you expect is to trick them into thinking that you're using a more traditional browser like Internet Explorer or Netscape's Firefox. To do this, you'll have to change the user-agent string that the HttpClient displays when it sends requests. The following example code shows how to easily do this:


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.params.HttpMethodParams;

...

HttpClient httpclient = new HttpClient();

httpclient.getParams().setParameter(HttpMethodParams.USER_AGENT,
     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

...


In this example, we're pretending to be Internet Explorer 6.0 running on Windows XP, but your HttpClient can pretend to be whatever browser you want. There's a handy list of user-agent strings on Wikipedia. More information about the Jakarta Commons Project and HttpClient can be found on the Jakarta Commons Website.

Jeffrey P. Bigham
ManticMoo.COM All Articles Jeff's Articles