jump to navigation

Java HTTP proxy settings February 18, 2011

Posted by Anoop Somasundaran in Java, Linux, Tomcat, Unix.
Tags: , , , ,
trackback

Some of you might have come across http connectivity issues especially when you setup your java application on staging or production servers. Due to security reasons, access to internet/external URLs within an organization is often through proxy servers. When I was trying to setup one of the java web applications on staging and production servers, the application was throwing an exception – ‘java.net.ConnectException: Connection refused’ when it tries to make an HTTP connection to get the content. However we were not getting this error in our development environment. I was almost sure that the problem could be because of the proxy servers used in the production and staging setup. The proxy server was blocking outbound traffic from the application servers.

After some trial and error attempts, I found the following solution to fix this issue.

I was using tomcat as the application server and I had to add the following lines in catalina.sh

JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyHost=ProxyURL"
JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyPort=ProxyPort"
JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyUser=UserName" (Optional)
JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyPassword=Password" (Optional)

Note: Please don’t copy and paste the above lines into Linux environment. The double quotes might give you issues on Linux if you copy double quotes from windows. I have faced this issue several times 😦

This problem can also be resolved by adding the following lines in your code.
System.getProperties().put("http.proxyHost", "ProxyURL");
System.getProperties().put("http.proxyPort", "ProxyPort");
System.getProperties().put("http.proxyUser", "UserName"); (Optional)
System.getProperties().put("http.proxyPassword", "Password"); (Optional)

Adding the settings at the server level seems to be a much better option though.

Advertisements

Comments»

No comments yet — be the first.

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

%d bloggers like this: