Java Networking Proxy


To address security issue, network administrators often install a special component called “proxy server.” It is essentially a service that sits between the Internet and the internal network and manages connections between the two worlds. This way, proxies reduce outside security threats while still allowing internal users to access Internet services.

However, Java applications, to be useful, must pass though these proxies. Fortunately, Java provides provisions to work with proxies. The solution lies in activating certain system properties in the Java runtime. Java application needs to specify information about the proxy itself as well as specify user information for authentication purposes. There are many ways we can do it.

1.1. Using Command Line Arguments

We can set the required information when starting the JVM for a JAVA application from the command line. For example, if the proxy process runs at port 8 0 8 0 in the machine having IP address, use the following command to start the application AnApp:

java –DproxyHost= –DproxyPort=8080 AnApp

If proxy requires authentication, use two additional arguments

java –DproxyHost= –DproxyPort=8080 –DproxyUser=user –

DproxyPassword=password AnApp

where user and password are the login and password required by the proxy respectively.

1.2. Using System Properties

Alternatively, the same information can be specified in the client programs as follows:

System.setProperty(“http.proxyHost”, “”);

System.setProperty(“http.proxyPort”, “8080”);

If proxy requires authentication, use the following two properties.

System.setProperty(“http.proxyUser”, “user”);

System.setProperty(“http.proxyPassword”, “password”);

1.3. Using Proxy Class

In Java 1.5 and later, we can also pass a instance to the openConnection() method of URL class. Add this code snippet in your Java network application that wants to use a proxy.

URL url = …

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(“”, 8080));

URLConnection con = url.openConnection(proxy);


This class is used if the information about the proxy is not known or there are many proxies and we are unable to determine a suitable proxy to be used. Specifically, it gives us the flexibility to:

  • Decide if a proxy should be used or not for a URL being used.
  • Select one (or more) to be used.
  • Manage failures when connecting to proxy servers.

Although, ProxySeiector class is an abstract class, we can use one provided by its getDefauit() method that returns system-wide proxy selector. However, to use system proxy selector, we must set a system property as follows:

System.setProperty(“”, “true”);

The list of available proxies for a given URL may be obtained as follows:

String urlStr = “”;

URL url = new URL(urlStr);

ProxySeiector ps = ProxySelector.getDefault();

java.util.List<Proxy> proxies = URI(urlStr));

Any one from this list may be used. To use the first one, we write the code

Proxy proxy = proxies.get(0);

URLConnection con = url.openConnection(proxy);

The entire steps can be summarized by a single statement as follows:

Proxy proxy = (Proxy) ProxySelector.getDefault().select(new URI(urlStr)). iterator().next();

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

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