如何使用 httpWebRequest 和方法 BeginGetRequestStream Windows Phone 7 管理超时

发布于 2024-11-10 08:04:52 字数 982 浏览 1 评论 0原文

我有这个例子,但我想知道如何准确管理这个例子的超时。请帮我。 提前致谢

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 技术交流群。

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

发布评论

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

评论(1

人│生佛魔见 2024-11-17 08:04:52

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 then Abort() the request and assume it timed out.

For more details and an example, see: HttpWebRequest Timeout in WP7 not working with timer

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