异步 WebRequest 超时 Windows Phone 7
我想知道 Windows Phone7 上 HttpWebRequest 超时的“正确”方法是什么?
我一直在阅读有关 ThreadPool.RegisterWaitForSingleObject() 的内容,但这不能用作 WaitHandles 在运行时抛出未实现的异常。
我也一直在研究 ManualReset 事件,但是 A)没有正确理解它们,B)不明白阻塞调用线程如何成为实现异步请求超时的可接受方法。
这是我现有的代码,没有超时,有人可以告诉我如何为此添加超时吗?
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C# 的基本异步超时模式:
不确定这是否适用于 Windows Phone 7。
您可能想看一下 -
向客户端代码公开异步功能:Windows Phone 7
Basic Async Timeout Pattern for C#:
Not sure if this will work on Windows Phone 7.
You might want to take a look at this -
Exposing asynchronous features to client code: Windows Phone 7