Handling proxy in Java Applications

This post has been updated on June 2012. If you have read this post before or have downloaded the related resources, please reconsider this post.
There was a bug in the previous resources (mistyping in the source codes).

Static proxy selection
If you know your proxy settings (i.e. proxy host, proxy port, authentication …), this is quite an easy task. Just set in your Java program your proxy settings through system properties as follows

   System.setProperty("http.proxyHost", proyHost);
   System.setProperty("http.proxyPort", proyPort);
   System.setProperty("http.proxySet", "true");


System.setProperty("https.proxyHost", proyHost);
System.setProperty("https.proxyPort", proyPort);
System.setProperty("https.proxySet", "true");

for HTTPS connections.

Note these same properties can be set on command line through

java -jar application.jar -Dhttp(s).proxyHost=myProxyHost -Dhttp(s).proxyPort=myProxyPort

Automatic proxy selection via proxy auto configuration “pac” script (.pac)
If proxy settings are set automatically via a proxy pac script (a script that is generally configured on web browsers like IE or else), one have to parse the auto configuration pac script to determine if a proxy is needed for a given targeted URL or not, and if so what is the proxy configuration to set ?

In this article (see in downloads section), I provide a “jar” that contains a utility Java class ProxyUtil that parses the auto-configuration proxy script and determines the adequate proxy settings for a given target URL as configured in the script.

In your Java code, call

// (old post, deprecated) ProxyUtil.getProxy(URL, URL): IProxyInfo
ProxyUtil.getProxy(URL, URL): java.net.Proxy

which returns a java.net.Proxy instance that contains the proxy host and proxy port metadata for the input target URL (say “http://www.worpress.com” or else). Please refer to the Java documentation for more information about the java.net.Proxy class.

ProxyUtil.getProxy takes the “proxy pac script” URL and target URL as inputs.¬†Once the proxy metadata obtained, one can set proxy parameters through System.setProperty(...) as explained earlier in this article.

Where can I find my proxy pac auto configuration script URL on Windows ?
On windows systems, the auto configuration pac URL is generally found (if any) in the windows registry under the path/key

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

Under the variable


To get the script URL, one can type on the Windows command line

reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "AutoConfigURL"



Special thanks to M. Broutin who helped me a lot to develop this part (might even say that he has done almost every part of it !)


5 thoughts on “Handling proxy in Java Applications”

    1. Thank you for your comment.
      I updated the post, cause I discovered a mistype error in the first one. I changed the way I did that. I used mozilla javascript implementation.
      I invite you to reconsider the post.


  1. This is awesome. saved me an hour in testing the two dozen or so proxies in my corporate intranet script over badly specified and largely overlapping IP ranges buried in a dozen deeply nested conditionals….

    any chance you can throw it on a public vcs with an open license? I think I might like to try to create an http-client/commons-http proxy selector wrapper.

  2. Thank you very much for your blog. This helped me to solve a connectivity problem which was a mystery for a long time. Your program helped me to identify the proxies being hit when we were facing connectivity issues and ask the right questions to the network support guys.


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s