WP7 中的 HttpWebRequest 超时不适用于计时器
由于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码按我的预期工作。当您对待处理的请求调用 Abort() 时,您的 ReadCallback 预计会触发。然后,当您调用 EndGetResponse() 时,您应该会收到 Status=RequestCanceled 的 WebException。
尝试稍微修改一下代码以查看其实际效果:
另请参阅 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:
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."