【发布时间】:2013-03-31 14:02:27
【问题描述】:
我正在尝试访问需要身份验证的 Web 服务,但我也在需要自己身份验证的代理后面。 能够通过代理服务器,因为当 Web 服务 URL 没有身份验证时我可以获得结果。但是,当 Web 服务需要身份验证时,它根本不起作用。我收到以下错误:
Exception in thread "main" java.net.ProtocolException: Server redirected too many times
下面是我的代码:
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
if(getRequestorType() == Authenticator.RequestorType.PROXY)
{ //for proxy
return (new PasswordAuthentication("user", "pass".toCharArray()));
}
else
{ // for web service
return (new PasswordAuthentication("user2", "pass2".toCharArray()));
}
}
};
Authenticator.setDefault(auth);
SocketAddress addr = new
InetSocketAddress("proxy addr", port no);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
URL url = new URL("...");
URLConnection yc = query.openConnection(proxy);
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
这是我在这里的第一个问题。任何帮助将不胜感激。
【问题讨论】:
-
您的请求调用进入了重定向循环,这可能有几个原因。为了缩小范围,您能否发布完整的 HTTP 请求 + 对服务的响应?
-
@DeepakBala 我收到响应代码 302,并且重定向位置与 http 请求 url 相同。顺便说一句,这是 HTTP 请求:api.datamarket.azure.com/Data.ashx/Bing/Search/…
标签: java web-services authentication proxy