Java的ProxySelector是否不能与自动代理配置脚本一起使用? [英] Does Java's ProxySelector not work with automatic proxy configuration scripts?
问题描述
我希望我的Java程序使用系统的代理配置.因此,我使用了很多地方的代码,例如此答案,将java.net.useSystemProxies
设置为true并调用ProxySelector.getDefault().select(...)
查找我要访问的特定主机的代理.当我在"Internet属性"中配置了单个显式代理服务器时,此方法可以正常工作.但是,如果我设置了使用自动配置脚本",它将始终返回直接的代理".
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";
}
,并且可以在我的浏览器中使用,但是ProxySelector.getDefault().select(...)
仍然仅返回DIRECT.
and it works in my browser, but ProxySelector.getDefault().select(...)
still returns only DIRECT.
我错过了什么吗? (应该在Java 1.6和Windows 7上使用).
Am I missing something? (This is on Java 1.6 & Windows 7, should it matter.)
推荐答案
No, the Java ProxySelector does not read Proxy Auto-Config (PAC) files.
但是,正如Brian de Alwis所建议的 作为对我的类似问题 代理Vole 库似乎提供了这种支持/功能.
However, as suggested by Brian de Alwis as an answer to my similar question, the Proxy Vole library appears to provide that support/capability.
为您的Java提供现成的网络连接 应用程序,您可以使用Proxy-Vole库.它提供了一些 自动检测当前代理设置的策略.有 许多可配置的策略可供选择.目前,代理- 田鼠支持以下代理检测策略.
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.
- 读取平台设置(支持:Windows,KDE,Gnome,OSX)
- 读取浏览器设置(支持:Firefox 3.x,Internet Explorer; Chrome和Webkit使用平台设置)
- 读取环境变量(在Linux/Unix服务器系统上经常使用的变量)
- 使用WPAD/PAC的自动检测脚本(不支持所有变体)
- 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)
这篇关于Java的ProxySelector是否不能与自动代理配置脚本一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!