【发布时间】:2015-05-05 21:07:51
【问题描述】:
我正在尝试通过经过身份验证的代理获取 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());
由于某种原因,身份验证进入重定向循环,因此结果是身份验证器打印“authenticating” 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)
代理正在使用给定的凭据,我已通过浏览器尝试过。 我试图让这个工作好几天,我尝试设置系统属性、apache httpclient 以及我可以从谷歌得到的任何东西。 任何想法表示赞赏。 :)
更新:
我使用 WireShark 进行了测试,代理身份验证详细信息在请求中,但代理返回 407 错误。同样,凭据还可以,它可以在浏览器中完美运行(我实际上是从源代码中复制了它们以确保)。
我注意到了一件事。 Proxy-Authorization头的值在浏览器和java发送的请求之间只有一个字符的区别。这意味着什么?
【问题讨论】:
-
我遇到了完全相同的问题...您有解决方案或任何新想法吗?
-
进一步:我读到了关于使用 Cookie-Hander 的文章,但即使启用了默认的 cookie-handler
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));我也会得到相同的结果... -
@Koocka 嗨,你成功了吗?
-
我也有同样的问题..
标签: java authentication proxy httpurlconnection basic-authentication