Tunneling all Java connections through a proxy server

If you’re behind your company’s proxy server and want to connect a Java application via TCP to another server you must provide the proxy configuration to your connection objects. Unfortunately you cannot configure many high-level objects in that way so you might want to set the proxy settings application wide. To achieve that you can simply change System.Properties on the fly. I used a simple static { } codeblock in one of my connection classes to ensure that the properties are set before any connection will be opened. Here’s some code:

System.setProperty("http.proxyHost", "proxy.host.com");
System.setProperty("http.proxyPort", "8080");

For more complicated usage scenarios you’ll find lots of best practices in the official document:
http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s