我正在尝试访问需要身份验证的 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();
这是我在这里的第一个问题。任何帮助将非常感激。