如何在Java中使用自动代理配置脚本 [英] How to use automatic proxy configuration script in Java

查看:910
本文介绍了如何在Java中使用自动代理配置脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Internet Explorer设置为具有用于Web访问的自动代理文件(所谓的PAC)。有没有办法在我的Java程序中使用它呢?

My Internet Explorer is set to have an automatic proxy file(so-called PAC) for web access. Is there a way to use this on my Java program, also ?

我的Java代码似乎根本不使用代理。

My below Java code does not seem to use proxy at all.

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

我还在Java控制面板上设置了我的代理脚本(Control-> Java) ,但结果相同。
我发现没有办法以编程方式为Java设置PAC文件。

I also set my proxy script on Java Control Panel(Control->Java), but the same result. and I found there's no way to set PAC file for Java programmatically.

人们使用http.proxyHost进行System.setProperties(..)但这是一个仅用于设置代理主机,而不是代理脚本(PAC文件)。

People use http.proxyHost for System.setProperties(..) but this is a only for setting proxy host, not proxy script(PAC file).

推荐答案

哇!我可以在Java上加载Proxy Auto-Config(PAC)文件。请参阅以下代码和包。

Wow! I could load Proxy Auto-Config (PAC) file on Java. Please see below codes and package.

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

You already have a [com.sun.deploy.net.proxy] package on your machine! Find [deploy.jar] ;D

这篇关于如何在Java中使用自动代理配置脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆