HttpUrlConnection代理身份验证进入重定向循环

Koo*_*cka 5 java authentication proxy httpurlconnection basic-authentication

我试图通过经过身份验证的代理获取URL的内容.这是我试图使用的代码:

        Authenticator authenticator = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                System.out.println("authenticating");
                return (new PasswordAuthentication("username", "password".toCharArray()));
            }
        };
        Authenticator.setDefault(authenticator);
        URL url = new URL("http://www.google.com");
        InetSocketAddress proxyAddress = new InetSocketAddress("address.of.proxy", 6060);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
        HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
        uc.connect();
        System.out.println(uc.getResponseCode());
Run Code Online (Sandbox Code Playgroud)

由于某种原因,身份验证进入重定向循环,因此结果是身份验证器打印"验证"20次,然后是ProtocolException

java.net.ProtocolException: Server redirected too many  times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1846)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at java.net.URLConnection.getContent(URLConnection.java:739)
at proxytest.RunThis.main(RunThis.java:29)
Run Code Online (Sandbox Code Playgroud)

代理正在使用给定的凭据,我已经通过浏览器尝试了它.我试图让这个工作好几天,我已经尝试设置系统属性,apache httpclient,以及任何我可以摆脱谷歌.任何想法都赞赏.:)

更新:

我使用WireShark进行了测试,代理身份验证详细信息在请求中,但代理返回407错误.再次,凭证是好的,它完全从浏览器工作(我实际上从源代码复制它们以确保).

但有一点我注意到了.Proxy-Authorization标头的值在浏览器和java发送的请求之间只有一个字符不同.这意味着什么吗?

小智 0

只需设置 JVM 属性:

jdk.http.auth.tunneling.disabledSchemes = ""
Run Code Online (Sandbox Code Playgroud)

请参阅:http://www.oracle.com/technetwork/java/javase/8u111-relnotes-3124969.html

把它留在这里,因为我花了一个小时研究这个问题。