Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "java.net.ProtocolException: Server redirected too many times" Error

I'm making a simple URL request with code like this:

URL url = new URL(webpage);
URLConnection urlConnection = url.openConnection();
InputStream is = urlConnection.getInputStream();

But on that last line, I'm getting the "redirected too many times error". If my "webpage" var is, say, google.com then it works fine, but when I try to use my servlet's URL then it fails. It seems I can adjust the number of times it follows the redirects (default is 20) with this:

System.setProperty("http.maxRedirects", "100");

But when I crank it up to, say, 100 it definitely takes longer to throw the error so I know it is trying. However, the URL to my servlet works fine in (any) browser and using the "persist" option in firebug it seems to only be redirecting once.

A bit more info on my servlet ... it is running in tomcat and fronted by apache using 'mod-proxy-ajp'. Also of note, it is using form authentication so any URL you enter should redirect you to the login page. As I said, this works correctly in all browsers, but for some reason the redirect isn't working with the URLConnection in Java 6.

Thanks for reading ... ideas?

like image 834
rjcarr Avatar asked Jun 13 '12 20:06

rjcarr


3 Answers

It's apparently redirecting in an infinite loop because you don't maintain the user session. The session is usually backed by a cookie. You need to create a CookieManager before you use URLConnection.

// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

// All the following subsequent URLConnections will use the same cookie manager.
URLConnection connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
// ...

See also:

  • Using java.net.URLConnection to fire and handle HTTP requests
like image 115
BalusC Avatar answered Nov 02 '22 17:11

BalusC


Duse, I have add this lines:

java.net.CookieManager cm = new java.net.CookieManager();
java.net.CookieHandler.setDefault(cm);

See this example:

java.net.CookieManager cm = new java.net.CookieManager();
java.net.CookieHandler.setDefault(cm);
String buf="";
dk = new DAKABrowser(input.getText());
try {
    URL url = new URL(dk.toURL(input.getText()));
    DataInputStream dis = new DataInputStream(url.openStream());
    String inputLine;
    while ((inputLine = dis.readLine()) != null) {
        buf+=inputLine;
        output.append(inputLine+"\n");
    }
    dis.close();
} 
catch (MalformedURLException me) {
    System.out.println("MalformedURLException: " + me);
}
catch (IOException ioe) {
    System.out.println("IOException: " + ioe);
}
titulo.setText(dk.getTitle(buf));
like image 5
Daniel Kennedy Avatar answered Nov 02 '22 16:11

Daniel Kennedy


I was using Jenkins on Tomcat6 on a unix environment and got this bug. For some reason, upgrading to Java7 solved it. I'd be interested to know exactly why that fixed it.

like image 1
user64141 Avatar answered Nov 02 '22 17:11

user64141