I want my Java program to use the system's proxy configuration. Accordingly, I used the code found in many places, such as this answer, to set java.net.useSystemProxies
to true and call ProxySelector.getDefault().select(...)
to discover the proxy for the particular host I want to reach. This works fine when I've configured a single explicit proxy server in Internet Properties. But if I have set "Use automatic configuration script", it always returns the DIRECT "proxy".
I know that the script works, as my browser can access the hosts for which it returns a proxy server, and without the script set, it can't. I even tried simplifying the script to its barest essentials:
function FindProxyForURL(url, host) { return "PROXY my.proxy.mydomain:3128"; }
and it works in my browser, but ProxySelector.getDefault().select(...)
still returns only DIRECT.
Am I missing something? (This is on Java 1.6 & Windows 7, should it matter.)
A proxy auto-config (PAC) file defines how web browsers and other user agents can automatically choose the appropriate proxy server (access method) for fetching a given URL.
To set up a proxy server connection automaticallySelect the Start button, then select Settings > Network & internet > Proxy. Under Automatic proxy setup, turn on Automatically detect settings.
When automatic proxy detection is enabled, the system attempts to locate a proxy configuration script that is responsible for returning the set of proxies that can be used for the request.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With