Search code examples
c#monowebrequest

How do I resend a "failed" WebRequest?


I send POST and GET WebRequest that should support longer periods of internet being down. The idea is to queue the failed (timedout) webrequest and to try to resend them periodically until the internet is up again and all queued WebRequests are sent.

However, I seems that I cannot just reuse the old WebRequest. Do I need to set it up again?

IAsyncResult result = request.BeginGetResponse (o => {
        callCallback (o);
}, state);

When request is just setup using:

var request = HttpWebRequest.Create (String.Format (@"{0}/{1}", baseServiceUrl, path)); 
request.Method = "GET";
request.ContentType = "application/xml; charset=UTF-8";
request.Headers.Add ("Authority", account.Session);
return request;

it works fine. But after a timeout (and request.Abort ();) and calling BeginGetResponse() on the same webrequest just freezes.


Solution

  • You cannot call BeginGetResponse() multiple times on the same HttpWebRequest. I'm not sure whether that's support on .NET, but it's not possible with Mono's implementation (and not something that'd be easy to change).

    See also Can I reuse HttpWebRequest without disconnecting from the server? - the underlying network connection will be reused.