locked
HttpWebRequest returns 404s for POST requests that return 302s only in Internet Explorer RRS feed

  • Question

  • I have a Silverlight (v3) application that uses WebRequest to make an HTTP POST request to a webpage on the same website as the Silverlight app. This HTTP request gets back a 302 (a redirect) to another page on the same website, which HttpWebRequest is automatically supposed to follow (according to the documentation).

    There's nothing particularly special about the code that makes the request (it uses the browser's HTTP stack, it is not configured to use the alternate inbuilt Silverlight HTTP stack):

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("{0}?name={1}&size={2}", _UploadUrl, Uri.EscapeUriString(Name), TotalBytes));
    request.Method = "POST";

    All this works fine in Firefox and Chrome; Silverlight makes the POST HTTP request, receives a 302 response and automatically does a GET HTTP request of the specified redirect URL and returns that to me (I know this because I used Fiddler to watch the HTTP requests going on). However, in Internet Explorer (v8), Silverlight does the POST HTTP request and then throws a WebException with a 404 error code!

    Using Fiddler, I can see that Silverlight/Internet Explorer was successfully returned the 302 status code for the request, and I assume that the 404 status code (and associated WebException) that I get in Silverlight is because as far as I know HTTP requests that are done via the browser stack can only return 200 or 404 due to limitations. The real question is why does Internet Explorer not follow through the redirect like the other browsers?

    I have discovered that Internet Explorer only exhibits this behaviour when you do a POST request. A GET request redirects successfully. This seems like pretty bad behaviour considering how many websites now do things in the Post-Redirect-Get style.

    I would prefer not to use the Silverlight client HTTP stack because to my knowledge requests issued by it do not include cookies that are a part of the browser's session, critically including the ASP.NET authentication cookie that I need to be attached to the HTTP requests being made by the Silverlight control.

    I have created a simple demo VS2008 solution which exhibits this weird behaviour, if it helps. It contains a basic ASP.NET MVC 1 project and a Silverlight 3 project. Navigate to the SilverlightControlTestPage.html page on the website to see the issue in action.

    Thanks in advance for any help!

    Sunday, November 28, 2010 4:58 AM

Answers

  • Hi,

    The WebRequest which supported in Silverlight is different in the .NET Framework, It is a quite simple version.

    The IE would not redirect automatically but FF did.

    At the moment, the workaround is not to use the RedirectToRoute, but return the View directly.

            public ActionResult RedirectMe()
            {
                return View("About");
                //return RedirectToRoute(new {controller = "Home", action = "About"});
            }


     

    Tuesday, November 30, 2010 1:57 AM