javaproxy

Does Java's ProxySelector not work with automatic proxy configuration scripts?


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.)


Solution

  • 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)

    Update 2024:

    proxy-vole has been forked from its original google code project to github under user markusbernhardt, and then more recently again been forked to a new github project under user akuhtz.

    Currently, the most updated version is published to the central maven repo with

    implementation("org.bidib.com.github.markusbernhardt:proxy-vole")
    

    or

    <dependency>
      <groupId>org.bidib.com.github.markusbernhardt</groupId>
      <artifactId>proxy-vole</artifactId>
    </dependency>
    

    check the latest version on github or the mvnrepository.com page.

    Since the package has already been forked several times due to lack of maintenance, it might be prudent to keep an eye out for any new forks.