HttpWebRequest Timeout in WP7 not working with timer

前端 未结 1 1197
既然无缘
既然无缘 2021-01-14 07:08

Since WP7 HttpWebRequest does not support timeout, I\'m using a timer to implement the functionality. Below is an example. I call GetConnection() from a UI form. But ReadCal

1条回答
  •  被撕碎了的回忆
    2021-01-14 07:56

    Your code works as expected for me. When you call Abort() on a pending request, your ReadCallback is expected to fire. Then when you call EndGetResponse() you should get a WebException with Status=RequestCanceled.

    Try this slightly modified code to see this in action:

    private void ReadCallback(IAsyncResult asynchronousResult)
    {
        _stopTimer = true;
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
    
        try
        {
            var m_response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            System.Diagnostics.Debug.WriteLine("Success");
        }
        catch (WebException exc)
        {
            System.Diagnostics.Debug.WriteLine(exc.Status);
        }
    }
    

    See also on MSDN: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.abort(v=VS.95).aspx

    "The Abort method cancels a request to a resource. After a request is canceled, calling the BeginGetResponse, EndGetResponse, BeginGetRequestStream, or EndGetRequestStream method causes a WebException with the Status property set to RequestCanceled."

    0 讨论(0)
提交回复
热议问题