Async WebRequest Timeout Windows Phone 7

Posted by Tyler on Stack Overflow See other posts from Stack Overflow or by Tyler
Published on 2011-03-13T08:07:11Z Indexed on 2011/03/13 8:10 UTC
Read the original article Hit count: 238

Hi All,

I'm wondering what the "right" way of timing out an HttpWebRequest is on Windows Phone7?

I've been reading about ThreadPool.RegisterWaitForSingleObject() but this can't be used as WaitHandles throw a Not implemented exception at run time.

I've also been looking at ManualReset events but A) Don't understand them properly and B) Don't understand how blocking the calling thread is an acceptable way to implement a time out on an Async request.

Here's my existing code sans timeout, can someone please show me how I would add a timeout to this?

public static void Get(Uri requestUri, HttpResponseReceived httpResponseReceivedCallback, ICredentials credentials, object userState, bool getResponseAsString = true, bool getResponseAsBytes = false)
                {
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);

                    httpWebRequest.Method = "GET";
                    httpWebRequest.Credentials = credentials;

                    var httpClientRequestState = new JsonHttpClientRequestState(null, userState, httpResponseReceivedCallback, httpWebRequest, getResponseAsString, getResponseAsBytes);

                    httpWebRequest.BeginGetResponse(ResponseReceived, httpClientRequestState);
                }

private static void ResponseReceived(IAsyncResult asyncResult)
{
      var httpClientRequestState = asyncResult.AsyncState as JsonHttpClientRequestState;

       Debug.Assert(httpClientRequestState != null, "httpClientRequestState cannot be null. Fatal error.");

       try
       {
           var webResponse  = (HttpWebResponse)httpClientRequestState.HttpWebRequest.EndGetResponse(asyncResult);

       }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about windows-phone-7