[英]Hard coding Proxy in Java for connecting to specific URL
I have a web app which has to connect to external Web Services (using Axis client). 我有一个Web应用程序,该应用程序必须连接到外部Web服务(使用Axis客户端)。 I have to perform 2 things: 我必须执行两件事:
use a direct connection when we perform a local call through a URI like 当我们通过URI执行本地呼叫时,使用直接连接
http://localhost:7001/webApp/getImg?id=22
Note that it works like a charm if we use the system properties (http.proxyHost, etc.) instead of the ProxySelector. 请注意,如果我们使用系统属性(http.proxyHost等)而不是ProxySelector,它就像一个超级按钮。
But thing is then all the local calls are routed to the Proxy Server, which I dont want. 但是,然后所有本地电话都路由到了代理服务器上,这是我所不希望的。
So, just a simple question - how to implement a ProxySelector
which performs in the same way as when using proxy system properties? 因此,只有一个简单的问题-如何实现与使用代理系统属性时执行方式相同的ProxySelector
?
You can achieve this with the system properties by also setting http.nonProxyHosts=localhost
. 您还可以通过设置http.nonProxyHosts=localhost
使用系统属性来实现。 If you want to do it with a ProxySelector
then try something like this 如果您想使用ProxySelector
进行操作,请尝试这样的操作
final Proxy PROXY = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress("my.proxy.server", 8080));
ProxySelector.setDefault(new ProxySelector() {
public List<Proxy> select(URI u) {
if(u != null && !"localhost".equals(u.getHost())) {
return Arrays.asList(PROXY, Proxy.NO_PROXY);
}
else {
return Collections.singletonList(Proxy.NO_PROXY);
}
}
public void connectFailed(URI u, SocketAddress sa, IOException e) {
// do nothing
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.