Aaron Johnson Now with 50% less caffeine!

Posted
14 November 2007 @ 4pm

Tagged
J2EE, Syndication, Systems Administration

Java, Commons HTTP Client and HTTP proxies

If you’re living at a giant corporation during the day and you want to browse the web you’re probably going through some sort of proxy to the outside world. Most of the time you don’t care, but if you’re writing a Java application that needs to access resources on the other side of said proxy (ie: the rest of the world), you’ll eventually end up over here. That wonderful document will hook you up with all your need to know about setting the proxy host, port and optionally a username and password for your proxy as long as you’re using URLConnection, HttpURLConnection or anything that deals with the class URL. If you’re really a go-getter you might even browse over here and read all about how to utilize those properties on the command line, in code or when you’re deployed inside of Tomcat.

Some of you won’t be so lucky: you’ll eventually want to use some advanced tools that abstract you away from having to fetch InputStreams to get your feeds and will instead depend on the Commons HTTP Client, which unfortunately (or fortunately depending on your point of view), doesn’t care about those nice little system properties that java.net.URL likes and instead goes off and uses sockets directly. No, instead you have to do something like this:

HttpClient client = new HttpClient();
HttpConnectionManager conManager = client.getHttpConnectionManager();
client.getHostConfiguration().setProxy("proxyserver.example.com", 8080);

and if you want to provide a username and password for said proxy:

HttpState state = new HttpState();
state.setProxyCredentials(null, null,
   new UsernamePasswordCredentials("username", "password"));
client.setState(state);

which is all fine and dandy but sometimes I just wish the world were simpler. Ya know?


2 Comments

Posted by
Binoy Debnath
12 December 2008 @ 1am

Very quick and usefull information. Thank you.


Posted by
Alan Kennedy
4 July 2009 @ 3am

Hi,

You may be interested in this database of open source HTTP proxies written in Java.

http://proxies.xhaus.com/java/

There’s also a list of open source HTTP proxies written in python.

http://proxies.xhaus.com/python

Regards,

Alan.


Leave a Comment

Links: 11-13-2007 Java ZipEntry bug on Windows