URL Connection via Proxy (the Java 1.5 way)

In enterprises/corporate environments, more often than not, connection to external servers/resources is via a proxy server. While developing applications for such environments, developers need to make sure that their code connects to external sites/resources via the proxy.

In this post, I restrict myself to the HTTP protocol.

The Java development platform provides multiple methods to make HTTP connections via a proxy server. However, prior to Java 1.5, there was just one way available: by setting System properties.

There are two steps involved:

1. Set the http.proxyHost, http.proxyPort, http.proxySet properties as follows:

Properties props = System.getProperties();
props.put("http.proxyPort","8080"); //proxy port
props.put("http.proxyHost","proxy.xyz.co.in"); //the proxy server name or IP
props.put("http.proxySet", "true");

2. Connect to the remote server. The properties set above would be automatically picked up by the JVM.

URLConnection urlConn = null;
BufferedReader reader = null;
String response = "";
String output = "";
URL url = new URL("www.google.com");
urlConn = url.openConnection();
urlConn.connect();
reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
response = reader.readLine();
while (response!=null) {
	output+= response;
	response = reader.readLine();
}	
System.out.println("Output: " + output);

For FTP, HTTPS and SOCKS the following system properties need to be set:

ftp.proxHost
ftp.proxyPort
ftp.nonProxyHosts 

htttps.proxyHost
https.proxyPort 

socksProxyHost
socksProxyPort

Although, this approach is very simple, there are some obvious drawbacks.
1. This is a all or none approach. Once the properties are set, till the time the properties are removed, all connections would happen via the same proxy server.
2. The properties are set VM-wide. This might causing site effects to other parts of the application that might not need the use of the proxy server.
3. If the corporate environment has the restriction that internal systems need to be accessed directly, whereas the external systems need to be accessed via the proxy, then this approach will fail.

With Java 1.5, the developer is provided with a more powerful and flexible approach via the Proxy class.

To define a HTTP proxy using this class, the following code is required:

String proxyHost = "proxy.xyz.co.in"; //replace with your proxy server name or IP
int proxyPort = 8080; //your proxy server port
SocketAddress addr = new InetSocketAddress(proxyHost, proxyPort);
Proxy httpProxy = new Proxy(Proxy.Type.HTTP, addr);

Now, we need to use the above defined proxy while opening a URL Connection. For this, the URL class provides a new overloaded method openConnection(Proxy proxy) that accepts a Proxy instance.

Below is the code for the same:

String proxyHost = "proxy.xyz.co.in"; //replace with your proxy server name or IP
int proxyPort = 8080; //your proxy server port
SocketAddress addr = new InetSocketAddress(proxyHost, proxyPort);
Proxy httpProxy = new Proxy(Proxy.Type.HTTP, addr);

URLConnection urlConn = null;
BufferedReader reader = null;
String response = "";
String output = "";
URL url = new URL("www.google.com");
//Pass the Proxy instance defined above, to the openConnection() method
urlConn = url.openConnection(httpProxy); 
urlConn.connect();
reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
response = reader.readLine();
while (response!=null) {
	output+= response;
	response = reader.readLine();
}	
System.out.println("Output: " + output);

That’s all. No system-wide settings are required.

If, for a particular connection, you want to connect directly (and not go via the proxy) – this would be required for connecting to internal servers, then use the following:

URL intranetURL = new URL("http://internal.xyz.co.in/");
URLConnection conn = intranetURL.openConnection(Proxy.NO_PROXY);

The Proxy.NO_PROXY static member forces the connection to retrieve the defined URL directly bypassing all proxy settings.

The proxy class also allows you to define a SOCKS proxy by using the type Proxy.Type.SOCKS.

Thus, as you can see, with Java 1.5 and above, the developer has more control while connecting via proxies.

Java 1.5 provides another class called the Proxy Selector which gives even more control by allowing dynamic selection of proxies. I will cover that in another post.

Advertisements
This entry was posted in Java and tagged , , , , , . Bookmark the permalink.

2 Responses to URL Connection via Proxy (the Java 1.5 way)

  1. ElGrosso says:

    where can i specify the username & password for the proxy?. I’m obtaining an 407 error. Thanks

  2. KP says:

    How can I make the http proxy to use the credentials passed by me rather than using the user credentials from the system ?

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