我实在无法回答这个问题。
如何在 Apache HttpClient 4.1.3 中设置 nonProxyHosts?
在旧的 Httpclient 3.x 中,这非常简单。您可以使用 set NonProxyHosts 方法来设置它。
但现在,新版本没有等效的方法。我一直在浏览 api 文档、教程和示例,但到目前为止还没有找到解决方案。
要设置普通代理,您可以这样做:
HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
有谁知道新版本 httpclient 4.1.3 中是否有用于设置 nonProxyHosts 的开箱即用解决方案,还是我必须自己做
if (targetHost.equals(nonProxyHost) {
dont use a proxy
}
提前致谢。
@moohkooh:这就是我解决问题的方法。
DefaultHttpClient client = new DefaultHttpClient();
//use same proxy as set in the system properties by setting up a routeplan
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(client.getConnectionManager().getSchemeRegistry(),
new LinkCheckerProxySelector());
client.setRoutePlanner(routePlanner);
然后你的 LinkcheckerProxySelector() 会喜欢这样的东西。
private class LinkCheckerProxySelector extends ProxySelector {
@Override
public List<Proxy> select(final URI uri) {
List<Proxy> proxyList = new ArrayList<Proxy>();
InetAddress addr = null;
try {
addr = InetAddress.getByName(uri.getHost());
} catch (UnknownHostException e) {
throw new HostNotFoundWrappedException(e);
}
byte[] ipAddr = addr.getAddress();
// Convert to dot representation
String ipAddrStr = "";
for (int i = 0; i < ipAddr.length; i++) {
if (i > 0) {
ipAddrStr += ".";
}
ipAddrStr += ipAddr[i] & 0xFF;
}
//only select a proxy, if URI starts with 10.*
if (!ipAddrStr.startsWith("10.")) {
return ProxySelector.getDefault().select(uri);
} else {
proxyList.add(Proxy.NO_PROXY);
}
return proxyList;
}
所以我希望这会对你有所帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)