The secret to combining Java and proxies lies in activating certain system properties in the Java runtime. These properties appear to be undocumented, and are whispered between programmers as part of the Java folklore. In order to work with a proxy, your Java application needs to specify information about the proxy itself as well as specify user information for authentication purposes. In your program, before you begin to work with any Internet protocols, you'll need to add the following lines:
System.getProperties().put( "proxySet", "true" ); System.getProperties().put( "proxyHost", "myProxyMachineName" ); System.getProperties().put( "proxyPort", "85" );
The first line above tells Java that you'll be using a proxy for your connections, the second line specifies the machine that the proxy lives on, and the third line indicates what port the proxy is listening on. Some proxies require a user to type in a username and password before Internet access is granted. You've probably encountered this behavior if you use a Web browser behind a firewall. Here's how to perform the authentication:
URLConnection connection = url.openConnection(); String password = "username:password"; String encodedPassword = base64Encode( password ); connection.setRequestProperty( "Proxy-Authorization", encodedPassword );
The idea behind the above code fragment is that you must adjust your HTTP header to send out your user information. This is
achieved with the setRequestProperty() call. This method allows you to manipulate the HTTP headers before the request is sent out. HTTP requires the user name and
password to be base64 encoded. Luckily, there are a couple of public domain APIs that will perform the encoding for you (see
the Resources section).
As you can see, there's not a whole lot to adding proxy support to your Java application. Given what you now know, and a little research (you'll have to find out how your proxy handles the protocol you're interested in and how to deal with user authentication), you can implement your proxy with other protocols.
PerfectlyBy Anonymous on October 3, 2009, 9:45 amThank you very much!!
Reply | Read entire comment
i dont get it, where am i supposed to put that in? im usually prBy Anonymous on August 11, 2009, 9:08 ami dont get it, where am i supposed to put that in? im usually pretty good with comps, but not really good with programing
Reply | Read entire comment
Good tipBy Anonymous on July 22, 2009, 5:36 amI reeally liked it very much. Thanks
Reply | Read entire comment
HTTP proxies written in java.By Anonymous on July 4, 2009, 6:54 amHi, 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...
Reply | Read entire comment
fixBy Anonymous on June 1, 2009, 7:05 pmin order for this to work add Basic keyword to the encoded password connection.setRequestProperty( "Proxy-Authorization", "Basic " + encodedPassword );
Reply | Read entire comment
View all comments