Java HTTP Proxy Settings

Overview

For local networks within an organization, access to the public-domain Internet is often via a HTTP Proxy. This article talks about the HTTP proxy settings for the Java environment. I did not find a good document on the Web to describe these settings; Had to discover many of them by trial-and-error. Hence this article.

Keywords

HTTP Proxy, Java Proxy Settings, Tomcat, Application Server, Servlets, HTTP Proxy Authentication for Java, Java Application Proxy Settings

Scenario

  • Your Java client runs on a machine on the Local network – Private LAN. The client could be a standalone application, or a servlet hosted on a web container like Tomcat
  • Your code access an external resource using HTTP. For example, invoking an external Web Service.
  • Your HTTP call needs to tunnel through the HTTP proxy (using SOCKS authentication). Even if authentication is not required, you would still need to configure the URL and the Port of your HTTP proxy.

Settings

Use one of the methods below for your JVM proxy settings. Try an alternate method if any particular method does not work. In most cases, you should not require any change the pre-compiled Java code for proxy settings. JVM’s environment settings should be enough to fix this problem.

Command Line JVM Settings

The proxy settings are given to the JVM via command line arguments:

 

$ java -Dhttp.proxyHost=proxyhostURL
-Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=someUserName
-Dhttp.proxyPassword=somePassword javaClassToRun

Setting System Properties in Code

Add the following lines in your Java code so that JVM uses the proxy to make HTTP calls. This would, of course, require you to recompile your Java source. (The other methods do not require any recompilation.):

System.getProperties().put("http.proxyHost", "someProxyURL");
System.getProperties().put("http.proxyPort", "someProxyPort");
System.getProperties().put("http.proxyUser", "someUserName");
System.getProperties().put("http.proxyPassword", "somePassword");

Don’t hardcode the proxy settings in your source. Read these settings from a configurable text file, so your users can configure them. You might also need to set this property:

System.getProperties().put("proxySet", "true");

Or

System.getProperties().put("http.proxySet", "true");

Tomcat Settings: catalina.properties

Append these properties to the catalina.properties file in Tomcat: ${CATALINA_OME}/conf/catalina.properties file:

http.proxyHost=yourProxyURL
http.proxyPort=yourProxyPort
http.proxyUser=yourUserName
http.proxyPassword=yourPassword

Tomcat Settings: catalina.bat

Add all the parameters defined above in the ${CATALINA_HOME}/bin/catalina.bat (for Windows) or ${CATALINA_HOME}/bin/catalina.bat (for *nix):

JAVA_OPTS="-Dhttp.proxyHost=yourProxyURL ..."

(Each option is seperated by spaces.)

References

18 Responses

  1. Good effort, using Proxy class gives us more freedom. You can refer this link Java Networking and Proxies

  2. Hello

    Great book. I just want to say what a fantastic thing you are doing! Good luck!

    G’night

  3. where to fill the domain? domain/yourUserName is failed

  4. [...] Java HTTP Proxy Settings « Information for Technology HTTP proxy configuration settings for Tomcat / Java. (categories: java tomcat proxy configuration proxyhost proxyport ) [...]

  5. [...] 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 [...]

  6. [...] Java HTTP Proxy Settings « Information for Technology For local networks within an organization, access to the public-domain Internet is often via a HTTP Proxy. This article talks about the HTTP proxy settings for the Java environment. (categories: java programming development proxy tomcat deployment ) Add this to:    [...]

  7. very interesting.
    i’m adding in RSS Reader

  8. Thanks, very useful for me :)

  9. so with this piece of code, i could integrate it with an application? and it would automatically go through the proxy? as long as i had access to the http proxy service?

  10. Thanx.. dude…
    helped me much….
    while working wid amazon ec2 and soap…

  11. Thanks for the info.

    Is it possible to specify a .pac script(Proxy Auto Configuration) instead of proxy host information?

  12. Thank you. I was looking for it for a long time!

  13. Imran, does your code above really work if the proxy requires authentication? I don’t think it does, note that http.proxyUser / http.proxyPassword are standard properties like http.proxyHost.

    http://java.sun.com/j2se/1.5.0/docs/guide/net/proxies.html

    You have to use a java.net.Authenticator, sample here:

    http://www.softonaut.com/2008/06/09/using-javanetauthenticator-for-proxy-authentication/

  14. Thanks Jamshid for providing additional details and links.

  15. Tanks!!!

    very very useful your information….!!!!

    Abraços…

  16. HI
    I am getting following error kindly help me

    java.io.IOException: Server returned HTTP response code: 407 for URL: http://wwwd.way2sms.com/auth.cl
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1236)
    at com.aswinanand.sms.SMS.send(SMS.java:76)
    at MeraSMS.main(MeraSMS.java:31)

  17. Hi
    I am also getting same problem ,any one kindly help to solve the below issue
    java.io.IOException: Server returned HTTP response code: 407 for URL: http://wwwd.way2sms.com/auth.cl
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1236)
    at com.aswinanand.sms.SMS.send(SMS.java:76)
    at MeraSMS.main(MeraSMS.java:31)

Leave a Reply