简体   繁体   English

为什么在HttpUrlConnection.plainConnect()中ProxySelector为null?

[英]Why does the ProxySelector is null in HttpUrlConnection.plainConnect()?

I have to call a WS that requires custom client authentication. 我必须调用需要自定义客户端身份验证的WS。 This authentication is done by a program running on the client and listening on http://127.0.0.1:80 . 身份验证是通过在客户端上运行并在http://127.0.0.1:80上侦听的程序完成的。 So I add a ProxySelector when starting up like this : 所以我像这样启动时添加了一个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);

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... 这可以正常工作,但是经过一些重构(与WS调用无关),而不是将http://my.server.com作为URI输入,而是使用了socket://my.server.com ,但失败了。 “未知代理类型:HTTP”,对于SOCKET方案来说似乎很正常...

The difference between my old application and the new one is the behavior during HttpUrlConnection.plainConnect() was not the same. 我的旧应用程序与新应用程序之间的区别是HttpUrlConnection.plainConnect()期间的行为不同。 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. 实际上,工作版本使用正确的URI( http://www.docjar.com/html/api/sun/net/www/protocol/http/HttpURLConnection.java.html的 922行)调用了我的ProxySelector。版本跳转到959行,并开始创建一个新的基础连接,该连接以socket://方案结束。

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. 这曾经使我的ProxySelector返回为“ sel”,但现在返回null。

Can someone explain me what exactly means these lines, and why the result is not the same than in my old app ? 有人可以向我解释这些行的确切含义,以及为什么结果与我的旧应用不同吗?

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. 用于生成WS客户端的jaxws-maven-plugin在正在运行的应用程序中的版本为1.10,在新版本中已更改为1.12,这导致了HttpUrlConnection的更改,如上所述。

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 :) 仍然不知道发生了什么,哪个依存库在1.10和1.12之间发生了变化,但是创建HttpConnections的方式有很大的不同:)

Thanks anyway for those who read my weird question... ^^ 无论如何,感谢那些阅读我的怪异问题的人... ^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sun.net.www.protocol.http.HttpURLConnection.plainConnect()中的NPE - NPE in sun.net.www.protocol.http.HttpURLConnection.plainConnect() 为什么 ProxySelector 在 Java 中返回不一致的结果? - Why is ProxySelector returning inconsistent results in Java? 为什么 HttpURLConnection 不发送 HTTP 请求 - Why does HttpURLConnection not send the HTTP request 为什么HTTPURLConnection.getInputStream()需要时间 - Why does HTTPURLConnection.getInputStream() takes time Java 的 ProxySelector 不能与自动代理配置脚本一起使用吗? - Does Java's ProxySelector not work with automatic proxy configuration scripts? HttpURLConnection urlConnection = null; (错误) - HttpURLConnection urlConnection = null; (Error) POST请求(HttpURLConnection)有趣的问题-但是为什么会发生呢? - Interesting issue with POST requests (HttpURLConnection) - But why does it happen? 除非我尝试接收某些内容,否则为什么HttpURLConnection不会发送数据 - Why HttpURLConnection does not send data unless I try to receive something 为什么HttpURLConnection在Android上失败但在Java Eclipse中成功? - Why does HttpURLConnection fail on android but succeed in java Eclipse? 为什么HttpURLConnection的getInputStream()方法返回RealBufferedSource类型的对象? - Why does the getInputStream() method of HttpURLConnection return an object of type RealBufferedSource?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM