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
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
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"
- Apache API for subnet calculations
- PAC (Wikipedia)
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 !)