Nitendra Gautam

Using Proxy in JVM based Applications

A proxy server, also known as a “proxy” or “application-level gateway”, is a computer that acts as a gateway between a local network (e.g., all the computers at one company or in one building) and a larger-scale network such as the Internet. Proxy servers provide increased performance and security. In some cases, they monitor employees’ use of outside resources.

As a software developer one has to work on development of multiple web application or a web service using Java or Scala. Majority of the production environment have some proxy set up in their server .Developers needs to include same set up in their code too in order to access URLs behind a firewall proxy and needs to exclude URL that does not need proxy to access it.

To use this code in Scala and java one needs to java.util.Properties library in their application.

Use the following code snippet in your application to set up proxy .

Lets assume our proxy in prod environment is proxy1 and port number is port1

Properties properties = System.getProperties();		
		 // Set the http proxy
			properties.setProperty("https.proxyHost", "proxy1");
			properties.setProperty("https.proxyPort", "port1");

After setting this proper in your application proxy will be set up through out the application.

Excluding proxy for single URL

Lets suppose URL quora quora is not behind proxy which you need to access normally from your code .To access this URL normally without ant proxy use the following code.


If you observe here i have removed some elements from the Url .All the elements like front slash “/” should be removed while adding Urls and Url having “.” should only be included

Excluding proxy for Multiple URL

Lets suppose you need to access multiple URL without proxy. Multiple Urls need to be be separated by pipe “” sign .Since we can only add the base Url wild card “*” expression is used to match the pattern accordingly .

Use the following snippet .


All the URL that we add in the system property using “http.proxyHosts” will be access normally .All the URL that you don’t add on this property will be accessed by a proxy.

Create a function with these settings and call once ,when a application starts .

In Java use below function.

public void getProxy() {
Properties properties =System.getProperties();		
 // Set the http proxy
  properties.setProperty("https.proxyHost", "proxy1");
properties.setProperty("https.proxyPort", "80");	

In scala use below function in a class or just create a function.

class ProxyUtility {
  def getProxy(): Unit= {
    val properties = System.getProperties
    //Set the httpProxy
    //Excluding the Hosts which does not Requires proxy

Reference :

Oracle Proxy Documentation