HTTP Proxy setting in Java. Setting up proxy.

java-cup-logoWorking behind a proxy and writing network related code has always been boring for me. Just because everytime I had to connect to Internet and get some data, I had to use Proxy settings. Whatever I used had a proxy configuration.

HTTP Proxy are configured mostly in corporate environments to manage internet usage. Hence if you are writing a code in Java to connect to internet and get something as we were doing in our Web crawler in Java article, we have to use HTTP proxy settings to get connected to internet.

When do you need to consider proxy settings for connecting to internet?

  1. 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
  2. Your code access an external resource using HTTP. For example, invoking an external Web Service.
  3. 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.

Sometime, you may encounter a compiled (Java) code that connects to network directly without considering http proxy settings. In such case you may not be able to run it behind your proxy environment. You can execute the code by setting few command line arguments to JVM.

Settings for HTTP Proxy

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 HelloWorldClass

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");

Hope this works. :)



11 Comments

  • EJP 15 May, 2009, 11:05

    # System.getProperties().put(\"http.proxyUser\", \"someUserName\");
    # System.getProperties().put(\"http.proxyPassword\", \"somePassword\");

    These properties do not exist in the JDK. Are you thinking of some 3rd-party package?

  • MJ 19 June, 2009, 21:55

    Just noticed that myself…

  • Alan Kennedy 4 July, 2009, 16:42

    Hi,

    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 proxies written in python.

    http://proxies.xhaus.com/python

    Regards,

    Alan.

  • Madhukar 10 August, 2009, 4:16

    If we are working from home, we may not need the proxy but yes if we are at work, most of us would definitely need to configure the proxy for anything to do with an url.

  • suganya murugesan 7 February, 2010, 15:04

    Hi,

    props.setProperty(“http.proxyHost”, “proxy.ssn.net”);
    props.setProperty(“http.proxyPort”, “8080”);
    props.setProperty( “mail.imap.socketFactory.class”, SSL_FACTORY);
    props.setProperty( “mail.imap.socketFactory.fallback”, “false”);
    props.setProperty( “mail.imap.port”, “993”);
    props.setProperty( “mail.imap.socketFactory.port”, “993”);
    props.put(“mail.imap.host”, “imap.gmail.com”);
    Session session =
    Session.getDefaultInstance(props, null);
    store = session.getStore(“imap”);
    store.connect(dialog.getServer(),dialog.getUsername(),dialog.getPassword());

    I could connect to gmail server without proxy setting. but when i set the proxy setting in college i couldn’t connect.

  • arsv mani kumar 19 August, 2012, 19:50

    thnX

  • chinna 4 October, 2012, 16:39

    Thank you very much for nice artical, you saved my day

  • munna 6 February, 2013, 10:05

    It was of no use to me :/

  • rku2013 15 May, 2013, 16:58

    We have created a war file and deployed in tomcat server.We are able to access using the urls,http://localhost:8080/test and http://:8080/test.This is accessible only in the particular LAN.how do I make it available outside LAN?Is there any possibility to update proxy settings in the host machine to accept the request from client?Any help is appreciated…..

  • ash 3 July, 2013, 19:13

    hi.. I have started tomcat with the -Dhttp.proxyHost=proxyhostURL and -Dhttp.proxyPort=proxyPortNumber parameters and it all works fine.

    But, the problem is when it comes to a https call, it does not work. I can see a connection timeout exception in the logs. I’m able to connect to URL via my browser and I have set the same proxy server and port while starting tomcat.

    Plz, can someone help..

    thanks

  • Thomas 6 July, 2013, 1:29

    Hello all, i need help…

    I have an application in tomcat server running under a proxy network, within the network, when a user login into the application via the proxy and a second user logs in(also via the proxy), the second user takes up the session of the first user, so at every point only one user can login.

    However, if the application is access directly(not via the proxy), multiple users can login and use the application gracefully.

    My question:

    Is there any configuration i need to do on the tomcat server to help manage this?
    Or
    Any suggestion on how to help resolved this issue.

    Any help will be greatly appreciated.

    Thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]

Current ye@r *