Skip to main content

Microsoft Silverlight

Unanswered Question HttpWebRequest infinite loop handling error 301: Moved PermanentlyRSS Feed

(0)

andulvar
andulvar

Member

Member

155 points

102 Posts

HttpWebRequest infinite loop handling error 301: Moved Permanently

I am using the asynchronous read capability of HttpWebRequest, like this:

        private void ReadHandler(object userdata)
{
object[] args = (object[])userdata;
HttpWebRequest w = (HttpWebRequest)args[0];
RequestState req = (RequestState)args[1];
IAsyncResult result = w.BeginGetResponse(new AsyncCallback(RespCallback), req);
}

 This works fine, unless the requested URL does not exist.  If the web server returns "404: Not Found", then the "RespCallback" is called and the call completes. 

However, if the web server returns "301: Moved Permanently" then the RespCallback is never called and the Silverlight application modifies the URL and repeats the request forever.  The sequence of requests as received by the web server looks like this:

The correct request comes first:

GET /Silverlight/Controls/TripleGauge.xaml?_sldummy=06f631f4-0527-4ec6-875a-013a5c576566 HTTP/1.1
Accept: */*
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: developers.cogentrts.com:8088
Connection: Keep-Alive

The first re-request.  Notice that the arguments have been stripped and a / added to the URL.  This is just plain wrong.  The arguments are a necessary part of the URL.

GET /Silverlight/Controls/TripleGauge.xaml/ HTTP/1.1
Accept: */*
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: developers.cogentrts.com:8088
Connection: Keep-Alive

Now a second / has been added to the URL and the arguments are still missing.

GET /Silverlight/Controls/TripleGauge.xaml// HTTP/1.1
Accept: */*
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: developers.cogentrts.com:8088
Connection: Keep-Alive

All subsequent requests look like the third one.  This repeats forever.

Since 301 is a permanent error, why is Silverlight making any repeated calls at all? 

Even if you can explain the re-try, why is it stripping the arguments, thereby potentially generating a completely different response page?

Why is it looping forever with the same URL?  Isn't the definition of insanity "repeating the same thing over and over, and expecting a different result"?

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24979 points

2,434 Posts

Microsoft

Re: HttpWebRequest infinite loop handling error 301: Moved Permanently

Hi Andulvar,

We conducted a 301 error from the target website and added this kind of the test code to see whether your issue can be reproduced.   


            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost:61928/Default.aspx"));
            wr.BeginGetResponse(result =>
            {
                HttpWebResponse hwr = (HttpWebResponse)wr.EndGetResponse(result);         //add a break point here

            }, wr);

It works as desired and not like your description.  For the asynchronous request, please follow this sample.

Best regards,

Jonathan

 

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

andulvar
andulvar

Member

Member

155 points

102 Posts

Re: Re: HttpWebRequest infinite loop handling error 301: Moved Permanently

I followed the code in one of the examples in the Silverlight documentation.  The fact that your test did not reproduce the error only tells us that you don't have a representative test.  I can tell you with absolute certainty that the request is being retried.  I ran the web server in a debugger and watched the requests as they arrived.  A single call to BeginGetResponse generates multiple web server hits, and they will repeat indefinitely so long as the web server continues to respond with 301.

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities