基于Java的web服务中使用代理获取网页
我的web服务将使用代理获取一些网页:基于Java的web服务中使用代理获取网页,java,web-services,tomcat,proxy,Java,Web Services,Tomcat,Proxy,我的web服务将使用代理获取一些网页: System.setProperty("java.net.useSystemProxies", "true"); List<Proxy> proxies = ProxySelector.getDefault().select(new URI(url)); URLConnection connection = null; Iterator<Proxy> iter = proxies.iterator(); do { Prox
System.setProperty("java.net.useSystemProxies", "true");
List<Proxy> proxies = ProxySelector.getDefault().select(new URI(url));
URLConnection connection = null;
Iterator<Proxy> iter = proxies.iterator();
do {
Proxy proxy = iter.next();
System.out.println(proxy);
try {
connection = new URL(url).openConnection(proxy);
} catch (IOException e) {
if (!iter.hasNext()) throw (e);
}
} while (connection == null);
System.setProperty(“java.net.useSystemProxies”,“true”);
List proxies=ProxySelector.getDefault().select(新URI(url));
URLConnection=null;
迭代器iter=proxies.Iterator();
做{
Proxy Proxy=iter.next();
System.out.println(代理);
试一试{
连接=新URL(URL).openConnection(代理);
}捕获(IOE异常){
如果(!iter.hasNext())抛出(e);
}
}while(connection==null);
但是,默认代理选择器始终返回直接连接(无代理)。我已经检查了IE代理设置,并将Java控制面板中的代理设置设置为系统浏览器设置
由于web服务是由Tomcat服务器调用的,因此可能必须在Tomcat服务器中进行一些配置?请查看此链接
- http.proxyHost:代理服务器的主机名
- http.proxyPort:端口号,默认值为80
- http.nonProxyHosts:应绕过代理直接访问的主机列表。这是由“|”分隔的模式列表。对于通配符,模式可以以“*”开头或结尾。任何匹配这些模式之一的主机都将通过直接连接而不是通过代理来访问