Does Java's ProxySelector not work with automatic proxy configuration scripts?
No, the Java ProxySelector does not read Proxy Auto-Config (PAC) files.
However, as suggested by Brian de Alwis as an answer to my similar question, the Proxy Vole library appears to provide that support/capability.
To provide network connectivity out of the box for you Java application you can use the Proxy - Vole library. It provides some strategies for autodetecting the current proxy settings. There are many configureable strategies to choose from. At the moment Proxy - Vole supports the following proxy detection strategies.
- Read platform settings (Supports: Windows, KDE, Gnome, OSX)
- Read browser setting (Supports: Firefox 3.x, Internet Explorer; Chrome and Webkit use the platform settings)
- Read environment variables (often used variables on Linux / Unix server systems)
- Autodetection script by using WPAD/PAC (Not all variations supported)
As already suggested by Mads Hansen, Proxy-Vole does the trick!
You just need to add the jar from the download site to your classpath (dlls are included) and this code helped me to configure the proxy stettings:
ProxySearch proxySearch = new ProxySearch();
proxySearch.addStrategy(Strategy.OS_DEFAULT);
proxySearch.addStrategy(Strategy.JAVA);
proxySearch.addStrategy(Strategy.BROWSER);
ProxySelector proxySelector = proxySearch.getProxySelector();
ProxySelector.setDefault(proxySelector);
URI home = URI.create("http://www.google.com");
System.out.println("ProxySelector: " + proxySelector);
System.out.println("URI: " + home);
List<Proxy> proxyList = proxySelector.select(home);
if (proxyList != null && !proxyList.isEmpty()) {
for (Proxy proxy : proxyList) {
System.out.println(proxy);
SocketAddress address = proxy.address();
if (address instanceof InetSocketAddress) {
String host = ((InetSocketAddress) address).getHostName();
String port = Integer.toString(((InetSocketAddress) address).getPort());
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", port);
}
}
}
I could load Proxy Auto-Config (PAC) file on Java. Please see below codes and package. I hope this would what you were looking for:
import com.sun.deploy.net.proxy.*;
.
.
BrowserProxyInfo b = new BrowserProxyInfo();
b.setType(ProxyType.AUTO);
b.setAutoConfigURL("http://yourhost/proxy.file.pac");
DummyAutoProxyHandler handler = new DummyAutoProxyHandler();
handler.init(b);
URL url = new URL("http://host_to_query");
ProxyInfo[] ps = handler.getProxyInfo(url);
for(ProxyInfo p : ps){
System.out.println(p.toString());
}
You already have a [com.sun.deploy.net.proxy] package on your machine! Find [deploy.jar] ;D