我有这个例子,但我想知道如何准确地管理这个例子的超时。请帮帮我。提前感谢
public void callREST()
{
Uri uri = new Uri("http://www.domain.com/RestService");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/xml";
request.BeginGetRequestStream(sendXML_RequestCallback, request);
}
private void sendXML_RequestCallback(IAsyncResult result)
{
var req = result.AsyncState as HttpWebRequest;
byte[] toSign = Encoding.GetEncoding("ISO-8859-1").GetBytes("<xml></xml>");
using (var strm = req.EndGetRequestStream(result))
{
strm.Write(toSign, 0, toSign.Length);
strm.Flush();
}
req.BeginGetResponse(this.fCallback, req);
}
private void fCallback(IAsyncResult result)
{
var req = result.AsyncState as HttpWebRequest;
var resp = req.EndGetResponse(result);
var strm = resp.GetResponseStream();
//Do something
}
Timeout
在Silverlight/Windows Phone 7中不支持作为HttpWebRequest的一部分。
您需要创建一个Timer
,并在启动请求的同时启动它。如果定时器在HWR返回之前触发,则Abort()
请求并假定它超时。
有关更多详细信息和示例,请参阅:WP7中的HttpWebRequest Timeout不与计时器一起工作