如何在Java中使用自动代理配置脚本
我的Internet Explorer设置为具有用于web访问的自动代理文件(所谓的PAC)。有没有一种方法可以在我的Java程序中使用它 我下面的Java代码似乎根本不使用代理如何在Java中使用自动代理配置脚本,java,proxy,pac,Java,Proxy,Pac,我的Internet Explorer设置为具有用于web访问的自动代理文件(所谓的PAC)。有没有一种方法可以在我的Java程序中使用它 我下面的Java代码似乎根本不使用代理 ArrayList<Proxy> ar = new ArrayList<Proxy>(ProxySelector.getDefault().select(new URI("http://service.myurlforproxy.com"))); for(Proxy p : ar){ Sys
ArrayList<Proxy> ar = new ArrayList<Proxy>(ProxySelector.getDefault().select(new URI("http://service.myurlforproxy.com")));
for(Proxy p : ar){
System.out.println(p.toString()); //output is just DIRECT T.T it should be PROXY.
}
ArrayList ar=new ArrayList(ProxySelector.getDefault().select(new URI()http://service.myurlforproxy.com")));
代表(代理人p:ar){
System.out.println(p.toString());//输出只是直接的T.T它应该是代理。
}
我还在Java控制面板(Control->Java)上设置代理脚本,但结果相同。
我发现无法通过编程方式为Java设置PAC文件
人们对System.setProperties(..)使用http.proxyHost,但这只是用于设置代理主机,而不是代理脚本(PAC文件)。Java没有任何内置的解析JS PAC文件的支持。你得靠自己。您可以下载该文件并从中解析代理主机。你应该读一读。哇!我可以在Java上加载代理自动配置(PAC)文件。请参阅以下代码和包装
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());
}
您的计算机上已经有[com.sun.deploy.net.proxy]包!
查找[deploy.jar];D在我的例子中,我刚刚计算出.pac文件将返回什么,然后是硬代码。基于@Jaeh answer,我使用了下面的代码。 请注意,SunAutoProxyHandler实现了AbstractAutoProxyHandler,还有一个名为PluginAutoProxyHandler的替代具体实现,但该实现似乎没有那么健壮:
BrowserProxyInfo b = new BrowserProxyInfo();
b.setType(ProxyType.AUTO);
b.setAutoConfigURL("http://example.com/proxy.pac");
SunAutoProxyHandler handler = new SunAutoProxyHandler();
handler.init(b);
ProxyInfo[] ps = handler.getProxyInfo(new URL(url));
for(ProxyInfo p : ps){
System.out.println(p.toString());
}
似乎您有一个DummyAutoProxyHandler类,上面有Pixie dust。您能否提供下载此libNeeded
SunAutoProxyHandler
(如另一个答案所示)和策略的链接。所有文件均如中所述。即:grant{permission java.security.AllPermission”“;}在policy.all
和Java中添加System.setProperty(“Java.security.policy”、“policy.all”)代码>。