Too many automatic redirections were attempted. RRS feed

  • Question

  • Get this error on the line : using (WebResponse response = request.GetResponse()). What do I need to correct to not get that exception error.

    This is the error I get on that line

    Error processing report 'MonthlyCardsOrderedRpt'.  Error: System.Net.WebException: Too many automatic redirections were attempted.
       at System.Net.HttpWebRequest.GetResponse()
       at VerfImport.Verfparser.ProcessReport(ReportExecutionService re, String ReportFileName, String ReportName, String ReportTitle, String SubClientId, DateTime rptStartDate, DateTime rptEndDate, DateTime rptRptDate)

    What do I need to correct to not get that exception error.

    WebRequest request = WebRequest.Create(requestUri);
                    var reportTimeoutMilliseconds = int.Parse(ConfigurationManager.AppSettings["ReportTimeoutSeconds"]) * 1000;
                    request.Timeout = reportTimeoutMilliseconds;
    //#if DEBUG
    //                request.UseDefaultCredentials = false;
    //                request.Credentials = new System.Net.NetworkCredential("tlode", "faster6", "srobo");
                    request.UseDefaultCredentials = true;
                    LogStep("Report '" + ReportName + "' for " + SubClientLabel + " - requesting.", false);
                    using (WebResponse response = request.GetResponse())
                    using (Stream readStream = response.GetResponseStream())
                    using (FileStream writeStream = new FileStream(OutputFileName, FileMode.Create))
                        readStream.ReadTimeout = reportTimeoutMilliseconds;
                        readStream.WriteTimeout = reportTimeoutMilliseconds;
                        byte[] readBuffer = new byte[4096];
                        int bytesRead = 0;
                        while ((bytesRead = readStream.Read(readBuffer, 0, readBuffer.Length)) != 0)
                            writeStream.Write(readBuffer, 0, bytesRead);

    Thursday, September 20, 2018 6:55 PM

All replies

  • On HttpWebRequest the property MaximumAutomaticRedirections determines the maximum redirects followed, the default is 50.

    Are you sure it is not an other problem, because more than 50 redirects seems excessive?

    Needs/Expect the server maybe some headers in the request (like agent/browser used).  

    Friday, September 21, 2018 5:58 AM
  • You likely don't need to change anything. What is happening is that you're going to that URL. The remote server is returning a redirect request. Your code is redirecting to the new URL. The new URL is returning a redirect request. Your code is redirecting to the new, new URL. Rinse and repeat. When you get to the max # of redirects it throws an exception because something is wrong. 

    In almost all cases where this happens the server is having an issue. Oftentimes this is just bad server code but it can also happen if the client-side scripts are not working properly. You should probably be able to replicate this in the browser pretty easily. 

    I say this is likely not your code because generally it indicates a site issue. You'll need to contact the side developers. However occasionally site developers may make assumptions that aren't necessarily true for WebRequest. For example the site might assume cookies are enabled or that some header is included. These are going to work automatically with WebRequest. You'll have to follow up with the site devs to find out what is going on but ultimately you might have to make a slight change to your request. But ultimately the site devs should be handling these scenarios and returning back proper errors, not redirecting indefinitely like is happening here.

    You might also use Fiddler or equivalent to watch the network requests. You should see the continual redirects and that might shed some light for the site devs.

    Michael Taylor

    Friday, September 21, 2018 1:58 PM
  • OK this is great information Michael. So you are saying that my code isn't the problem but it's remote server issue? You also say it indicates that it is a site issue. What site would that be? Should that site be in the code, where would I find that site name. I didn't create the site nor do I maintain it but I just want to be clear on how to find out the site name?
    Friday, September 21, 2018 4:28 PM
  • It's the site you're calling. In your code that is whatever requestUri is set to.

    Most likely it is a server issue as the server should only issue a redirect once. An infinite redirect tends to indicate something is wrong on the other side. But, again, contacting the site owners should help clarify what is going wrong.

    Michael Taylor

    Friday, September 21, 2018 5:07 PM
  • Thank you
    Friday, September 21, 2018 6:52 PM