WP7 中的 HttpWebRequest 超时不适用于计时器

发布于 2024-10-30 16:40:03 字数 1089 浏览 0 评论 0原文

由于 WP7 HttpWebRequest 不支持超时,因此我使用计时器来实现该功能。下面是一个例子。我从 UI 表单调​​用 GetConnection()。但在计时器时间结束之前,ReadCallback() 永远不会执行。一旦计时器停止,就会触发 ReadCallBack()。看起来计时器线程正在阻塞 HTTP 响应。任何帮助表示赞赏。我也尝试过 ManualResetEvent ,也有相同的结果。

private HttpWebRequest conn;
private bool _timedOut = false;
private DispatcherTimer tmr;

public void GetConnection()
{
    conn = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://www.contoso.com"));
    conn.Method = "GET";

    tmr = new DispatcherTimer();
    tmr.Interval = TimeSpan.FromSeconds(10);
    tmr.Tick += new EventHandler(tmr_Tick);
    _stopTimer = false;

    IAsyncResult resp = conn.BeginGetResponse(new AsyncCallback(ReadCallback), conn);

    tmr.Start();
}

private void tmr_Tick(object sender, EventArgs e)
{
   if (!_stopTimer)
   {
       tmr.Stop();
       conn.Abort();
   }
}

private void ReadCallback(IAsyncResult asynchronousResult)
{
    _stopTimer = true;
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    m_response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
}

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 ReadCallback() is never executed till the timer time is over. Once the timer is stopped, then ReadCallBack() is triggered. Seems like the timer thread is blocking the HTTP response. Any help is appreciated. I've also tried ManualResetEvent and that has the same result too.

private HttpWebRequest conn;
private bool _timedOut = false;
private DispatcherTimer tmr;

public void GetConnection()
{
    conn = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://www.contoso.com"));
    conn.Method = "GET";

    tmr = new DispatcherTimer();
    tmr.Interval = TimeSpan.FromSeconds(10);
    tmr.Tick += new EventHandler(tmr_Tick);
    _stopTimer = false;

    IAsyncResult resp = conn.BeginGetResponse(new AsyncCallback(ReadCallback), conn);

    tmr.Start();
}

private void tmr_Tick(object sender, EventArgs e)
{
   if (!_stopTimer)
   {
       tmr.Stop();
       conn.Abort();
   }
}

private void ReadCallback(IAsyncResult asynchronousResult)
{
    _stopTimer = true;
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    m_response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2024-11-06 16:40:03

您的代码按我的预期工作。当您对待处理的请求调用 Abort() 时,您的 ReadCallback 预计会触发。然后,当您调用 EndGetResponse() 时,您应该会收到 Status=RequestCanceled 的 WebException。

尝试稍微修改一下代码以查看其实际效果:

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);
    }
}

另请参阅 MSDN:
http://msdn。 microsoft.com/en-us/library/system.net.httpwebrequest.abort(v=VS.95).aspx

"Abort 方法取消对资源的请求。取消请求后,调用 BeginGetResponse、EndGetResponse、BeginGetRequestStream 或 EndGetRequestStream 方法会导致 WebException,且 Status 属性设置为 RequestCanceled。

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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文