为什么 HttpUrlConnection.plainConnect() 中的 ProxySelector 为 null?
我必须调用需要自定义客户端身份验证的 WS。此身份验证是由客户端上运行并侦听 http://127.0.0.1:80 的程序完成的。 因此,我在启动时添加一个 ProxySelector,如下所示:
final ProxySelector ps = new ProxySelector() {
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
ioe.printStackTrace();
}
@Override
public List<Proxy> select(URI uri) {
final List<Proxy> proxy = new ArrayList<Proxy>();
final SocketAddress adr = new InetSocketAddress("127.0.0.1", 80);
final Proxy p = new Proxy(Proxy.Type.HTTP, adr);
proxy.add(p);
return proxy;
};
ProxySelector.setDefault(ps);
这可以正常工作,但经过一些重构(与 WS 调用无关)后,而不是使用 http://my.server.com 作为 URI输入,我有 socket://my.server.com 并且它失败并显示“未知代理类型:HTTP”,这对于 SOCKET 方案来说似乎很正常......
我的旧应用程序和新的行为是在HttpUrlConnection.plainConnect() 不一样。事实上,工作版本使用正确的 URI 调用我的 ProxySelector (
所以区别在于以下几行:
ProxySelector sel =
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<ProxySelector>() {
public ProxySelector run() {
return ProxySelector.getDefault();
}
});
这曾经将我的 ProxySelector 返回为“sel”,但现在返回 null。
有人可以解释一下这些行的确切含义,以及为什么结果与我的旧应用程序中的结果不同?
I have to call a WS that requires custom client authentication. This authentication is done by a program running on the client and listening on http://127.0.0.1:80.
So I add a ProxySelector when starting up like this :
final ProxySelector ps = new ProxySelector() {
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
ioe.printStackTrace();
}
@Override
public List<Proxy> select(URI uri) {
final List<Proxy> proxy = new ArrayList<Proxy>();
final SocketAddress adr = new InetSocketAddress("127.0.0.1", 80);
final Proxy p = new Proxy(Proxy.Type.HTTP, adr);
proxy.add(p);
return proxy;
};
ProxySelector.setDefault(ps);
This use to work fine, but after some refactoring (not related to WS calls), instead of having http://my.server.com as URI input, I have socket://my.server.com and it fails with a "Unknown proxy type : HTTP", what seems quite normal with SOCKET scheme...
The difference between my old application and the new one is the behavior during HttpUrlConnection.plainConnect() was not the same. Indeed, the working version calls my ProxySelector with the right URI (line 922 of http://www.docjar.com/html/api/sun/net/www/protocol/http/HttpURLConnection.java.html),
whereas the new version jump to line 959 and start creating a new underlying connection, which ends up with a socket:// scheme.
So the difference lies in following lines :
ProxySelector sel =
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<ProxySelector>() {
public ProxySelector run() {
return ProxySelector.getDefault();
}
});
This used to return my ProxySelector as "sel" but now returns null.
Can someone explain me what exactly means these lines, and why the result is not the same than in my old app ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终,我想通了这一点!
用于生成 WS 客户端的 jaxws-maven-plugin 在工作应用程序中为 1.10 版本,在新版本中更改为 1.12,从而引入了 HttpUrlConnection 中的更改,如上所述。
仍然不知道发生了什么,以及哪个依赖库在 1.10 和 1.12 之间发生了变化,但是创建 HttpConnections 的方式有很大的差异:)
无论如何,感谢那些阅读我的奇怪问题的人... ^^
Eventually, I figured this out !
The jaxws-maven-plugin used to generate WS client was in version 1.10 in the working application, and changed to 1.12 in the new one, what introduced the changes in HttpUrlConnection as explained above.
Still don't know what happened, and which dependent library has changed between 1.10 and 1.12 but there is a quite BIG difference in the way of creating HttpConnections :)
Thanks anyway for those who read my weird question... ^^