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

About these ads

27 thoughts on “Java HTTP Proxy Settings

  1. Pingback: Aaron Johnson » Blog Archive » Links: 11-13-2007

  2. Pingback: Aaron Johnson » Blog Archive » Java, Commons HTTP Client and HTTP proxies

  3. Pingback: Links: 11-14-2007 :: the correct shade of blue

  4. 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?

  5. 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)

  6. 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)

  7. Several errors here.

    1. The proxySet property has never existed in the JDK, so setting it to ‘true’ or ‘false’ or ‘fred’ does nothing. It was part of the now very defunct HotJavabean (1997) and leaked into various books.

    2. The http.proxyUser and http.proxyPassword properties aren’t part of the JDK either. I believe they are part of one of the Apache libraries.

  8. We have a automatic proxy configuration file “***.pac” file.

    There is no user name or password,

    Does anyone know how to code for this situation?

  9. Hi I am running a standalone program as proxy server and provided all properties, but it is accepting though the user name is wrong.

    /***************************************************************************
    * Copyright (C)2011 Deutsche Bank AG APHO
    * %created_by: Vijender D (devvije) %
    * %derived_by: %
    * %date_created: 3:29:17 PM May 12, 2011 %
    * %date_modified: %
    * %state: %
    * %full_filespec: %
    * %release: %
    *
    * Author: Vijender D (devvije)
    *
    * Created on: May 12, 2011
    *
    * Purpose:
    *
    * Change History:
    * Date Author Reason
    * ————————————————————————–
    * May 12, 2011 Vijender D
    *
    ***************************************************************************/
    /**
    * @author devvije
    *
    */

    import java.io.*;
    import java.net.*;

    public class SimpleProxyServer {
    public static void main(String[] args) throws IOException {
    try {
    String localhost = “10.172.85.193″;
    String remotehost = “10.171.160.22″;
    int remoteport = 6666;
    int localport = 6666;
    // Print a start-up message
    System.out.println(“Starting proxy for ” + localhost + “:” + remoteport
    + ” on port ” + localport);

    System.getProperties().put(“http.proxyHost”, localhost);
    System.getProperties().put(“http.proxyPort”, localport);
    System.getProperties().put(“http.proxyUser”, “vijender”);
    System.getProperties().put(“http.proxyPassword”, “vijender123″);
    System.getProperties().put(“proxySet”, “true”);
    System.getProperties().put(“http.proxySet”, “true”);

    // And start running the server
    runServer(remotehost, remoteport, localport); // never returns
    } catch (Exception e) {
    System.err.println(e);
    }
    }

    /**
    * runs a single-threaded proxy server on
    * the specified local port. It never returns.
    */
    public static void runServer(String remotehost, int remoteport, int localport)
    throws IOException {
    // Create a ServerSocket to listen for connections with
    ServerSocket ss = new ServerSocket(localport);

    final byte[] request = new byte[1024];
    byte[] reply = new byte[4096];

    while (true) {
    Socket client = null, server = null;
    try {
    // Wait for a connection on the local port
    client = ss.accept();

    final InputStream streamFromClient = client.getInputStream();
    final OutputStream streamToClient = client.getOutputStream();

    // Make a connection to the real server.
    // If we cannot connect to the server, send an error to the
    // client, disconnect, and continue waiting for connections.
    try {
    server = new Socket(remotehost, remoteport);
    } catch (IOException e) {
    PrintWriter out = new PrintWriter(streamToClient);
    out.print(“Proxy server cannot connect to ” + remotehost + “:”
    + remoteport + “:\n” + e + “\n”);
    out.flush();
    client.close();
    continue;
    }

    // Get server streams.
    final InputStream streamFromServer = server.getInputStream();
    final OutputStream streamToServer = server.getOutputStream();

    // a thread to read the client’s requests and pass them
    // to the server. A separate thread for asynchronous.
    Thread t = new Thread() {
    public void run() {
    int bytesRead;
    try {
    while ((bytesRead = streamFromClient.read(request)) != -1) {
    streamToServer.write(request, 0, bytesRead);
    streamToServer.flush();
    }
    } catch (IOException e) {
    }

    // the client closed the connection to us, so close our
    // connection to the server.
    try {
    streamToServer.close();
    } catch (IOException e) {
    }
    }
    };

    // Start the client-to-server request thread running
    t.start();

    // Read the server’s responses
    // and pass them back to the client.
    int bytesRead;
    try {
    while ((bytesRead = streamFromServer.read(reply)) != -1) {
    streamToClient.write(reply, 0, bytesRead);
    streamToClient.flush();
    }
    } catch (IOException e) {
    }

    // The server closed its connection to us, so we close our
    // connection to our client.
    streamToClient.close();
    } catch (IOException e) {
    System.err.println(e);
    } finally {
    try {
    if (server != null)
    server.close();
    if (client != null)
    client.close();
    } catch (IOException e) {
    }
    }
    }
    }
    }

  10. Hiya very nice site!! Guy .. Excellent .. Amazing .. I’ll bookmark your site and take the feeds additionally?I’m glad to search out numerous useful information right here within the post, we’d like work out more techniques in this regard, thanks for sharing. . . . . .

  11. Pingback: jvm http proxy – wiloon

  12. There is NO SUCH THING as http.proxyUser and http.proxyPassword! They don’t exist. Proxies with authentication will not work with these commands.

  13. Excellent post. I was checking continuously this blog and I am impressed!
    Very helpful info particularly the last part :
    ) I care for such info a lot. I was seeking this particular information for a long time.
    Thank you and good luck.

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