I am trying to access a web service that requires authentication but I am also behind a proxy which requires its own authentication. Able to get pass the proxy server as I can get results when the web service URL has no authentication. However when a web service requires authentication it simply does not work. i get the following error:
Exception in thread "main" java.net.ProtocolException: Server redirected too many times
Here's my code below:
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();
This is my first question here. Any help would be much appreciated.