2

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.

2
  • Your request call went into a redirect loop which can happen for a couple of reasons. To narrow them down can you please post the full HTTP request + response made to the service ? Commented Mar 31, 2013 at 15:59
  • @DeepakBala i am getting response code 302 and the redirect location is the same as the http request url. btw here's the HTTP request: api.datamarket.azure.com/Data.ashx/Bing/Search/… Commented Mar 31, 2013 at 18:43

1 Answer 1

2

The URLs are not the same. The request URL uses HTTP and the redirect URL uses HTTPS. What is probably happening is that the service sends the redirect request to the proxy, but the proxy still keeps requesting the HTTP URL over and over. Some proxies are known to exhibit this kind of behavior.

Switch to using HTTPS in the initial request. My HTTP calls to the server in question resulted in a redirect to HTTPS while the HTTPS calls resulted in a 401.

HTTP/1.1 401 The authorization type you provided is not supported. Only Basic and OAuth are supported

Sign up to request clarification or add additional context in comments.

1 Comment

tried your suggestion and it worked! can't thank you enough for that. i just looked back at the documentation for the server and it said to use https - i have no clue how i missed that. However the authorization seems to be working for me, i don't understand much about Basic and OAuth but will look it up to learn more. You saved a lot of my time. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.