Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-01-2011, 06:38 AM   #1
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Java: Too many redirects on HTTP connection

I'm trying to open a web page to automate some data checking, and I'm getting a "too many redirects" exception. I'm not experienced enough with Java to know what to try next, and would like some help.

I'll show the code for what I've tried, but first, details about the website:
The website is a series of aspx pages. Going to the above address in a browser gets you to "default.aspx"--a search page. You don't see the redirects (specifically to a Login.aspx page--which redirects back to default.aspx). I can get the search page with either Firefox or wget--and on the same computer that I'm writing the Java code on. So, Firefox and wget are doing something that my code isn't. Also, I'm not behind a proxy.

I've searched about the redirect exception, and one or two pages blame poor website development. That may be true, but I don't have any control over the website. So, fixing the website is not an option.

I did find this page where someone has the same issue. I used some code given in one of the responses to discover the default.aspx->Login.aspx->default.aspx redirection loop.

Much of my code was pulled from the Working with URLs and Working with Cookies tutorials from Oracle. I tried adding the cookie handler thinking that maybe Login.aspx was trying to create a session ID or some other connection-specific identifier. But either cookies are not the solution OR I just didn't code it the right way.

So here's the code. It's ugly (in an every-branch-of-the-ugly-tree way):
package dallasinformationstatus;

import java.util.List;

public class Main {

    public static void main(String[] args) throws Exception {

        CookieManager cookieMonster = new CookieManager();
        List<HttpCookie> cookieJar;
        HttpCookie chocolateCookie;

/* Various addresses that end up falling into the redirect loop */
//        URI dallasURI = new URI("");
//        URI dallasURI = new URI("");
        URI dallasURI = new URI("");
//        URI dallasURI = new URI("");
        URL dallasURL = dallasURI.toURL();

        /* Comment the line below to generate the redirect exception.
           It is left uncommented here because I was using it to find out where
           the code was redirected to for each of the URIs listed above */
        HttpURLConnection dallasConnection = null;
        try {
          dallasConnection = (HttpURLConnection) dallasURL.openConnection();
          System.out.println("Response code = " + dallasConnection.getResponseCode());
          String header = dallasConnection.getHeaderField("location");
          if (header != null)
              System.out.println("Redirected to " + header);

           BufferedReader in = new BufferedReader(
				  new InputStreamReader(

	  String inputLine;

	  while ((inputLine = in.readLine()) != null)

        } catch (Exception e ) {
          System.out.println("EXCEPTION: " + e.getMessage() );
          if( dallasConnection != null ) {
            System.out.println("Response code = " + dallasConnection.getResponseCode());
          String header = dallasConnection.getHeaderField("location");
          if (header != null)
            System.out.println("Redirected to " + header);

          /* Cookie stuff added here in the vain hope that I would see
             SOMETHING being stored by the website to give me some
             clue about what to do next, but I never get anything
             other than no cookies :( */
          cookieJar = cookieMonster.getCookieStore().getCookies();
          if( cookieJar.isEmpty() ) {
            System.out.println("-->NO<-- cookie!");
          } else {
            chocolateCookie = cookieJar.get(0);
            System.out.println("COOKIE:\n  Domain: " +
                    chocolateCookie.getDomain() + "\n  Name: " +
                    chocolateCookie.getName() + "\n  Value: " +
                    chocolateCookie.getValue() );


Like I said, I don't know what else to try. I need to get this done using Java--no shell scripts, no calls to wget within Java, etc. Sample code would be great, but pointing me to some documentation would be just as good.

Thanks for any help (and for reading this wall of text).

After more searching, I'm 99% certain it's a cookie-handling issue. I added some more code (not included in the above) that examines the full response from the redirect to the Login.aspx page. The response includes a Set-Cookie header for "ASP.NET_SessionId". Now to find some code that will store and send the session ID.

Last edited by Dark_Helmet; 02-01-2011 at 03:54 PM.
Old 02-01-2011, 05:21 PM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Original Poster
Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Posting a reply instead of an additional edit to take the post off the 0-reply list.

The problem was cookie-handling. The Login.aspx page was trying to set two cookies: a session id and a public access value. Both cookies needed to be sent to the server when requesting default.aspx.

I added code to send the cookies like so:
if( ( !wrapper.sessionID.equals("Null") && ( !wrapper.publicAccess.equals("Null") ) )
                                            SESSION_ID_TAG + "=" + wrapper.sessionID + "; " +
                                            PUBLIC_ACCESS_TAG + "=" + wrapper.publicAccess + ";" );
SESSION_ID_TAG and PUBLIC_ACCESS_TAG are just constant Strings and wrapper is just a simple object to hold related values.

Though, I don't understand why the Oracle tutorial didn't mention the other half of handling cookies in their tutorial. It's one thing to receive a cookie, but you have to do something with it eventually. And I didn't see any code or hint in their tutorial on how to send the cookies back.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java - HTTP Status 500 fullgore Programming 9 01-02-2009 03:20 PM
Clicking on a review # redirects to Mega Man X LQ Suggestions & Feedback 3 07-15-2008 06:55 AM
Redirect HTTP by using JAVA WildBoy Programming 1 06-09-2008 01:47 AM
java.lang.InstantiationException: java.sql.Connection poeta_boy Programming 2 07-06-2005 08:26 PM
Firewall That Can Do HTTP NAT Redirects ebiven Linux - Security 2 12-13-2004 05:14 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration