如何使用 httpWebRequest 和方法 BeginGetRequestStream Windows Phone 7 管理超时
我有这个例子,但我想知道如何准确管理这个例子的超时。请帮我。 提前致谢
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
}
I have this example working but I want to know how manage timeout for this example exactly. Please help me.
Thanks in advance
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Silverlight / Windows Phone 7 中不支持将
Timeout
作为 HttpWebRequest 的一部分。您需要创建一个
Timer
并在启动请求的同时启动它。如果计时器在 HWR 返回之前触发,则Abort()
请求并假定超时。有关更多详细信息和示例,请参阅:HttpWebRequest 超时WP7 无法使用计时器
Timeout
isn't supported as part of HttpWebRequest in Silverlight / Windows Phone 7.You'll need to create a
Timer
and start that at the same time you start the request. If the timer fires before the HWR returns thenAbort()
the request and assume it timed out.For more details and an example, see: HttpWebRequest Timeout in WP7 not working with timer