Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 987
vanmelle Avatar asked Apr 25 '12 23:04

vanmelle


People also ask

What is automatic configuration script for proxy?

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.

How do I set automatic proxy settings?

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.

How does proxy Auto Detect work?

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.


1 Answers

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)
like image 54
Mads Hansen Avatar answered Oct 04 '22 14:10

Mads Hansen