如何在 Windows Phone 8 应用程序中为 httpwebrequest 设置超时? [英] How to set Timeout for httpwebrequest in windows phone 8 app?

查看:24
本文介绍了如何在 Windows Phone 8 应用程序中为 httpwebrequest 设置超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Windows Phone 8 应用程序,在我的应用程序中,我正在调用服务并将一些数据下载到我的应用程序中.

i am developing an windows phone 8 app , in my app i am calling services and downloading some data into my app .

我正在使用 httpwebrequest 进行请求,但我无法为我的 httpwebrequest 对象设置超时.

i am using httpwebrequest for request, but i am not able to set timeout to my httpwebrequest object.

这就是我创建和使用我的 httpwebrequest 的方式:-

This is how i have created and used my httpwebrequest :-

public async Task<string> ServiceRequest(string serviceurl, string request, string methodname)
        {
            string response = "";
            try
            {

                var httpwebrequest = WebRequest.Create(new Uri(serviceurl)) as HttpWebRequest;
                httpwebrequest.Method = "POST";
                httpwebrequest.Headers["SOAPAction"] = "http://tempuri.org/" + iTestservice + "/" + methodname + "";
                httpwebrequest.ContentType = "text/xml";


                byte[] data = Encoding.UTF8.GetBytes(request);
                using (var requestStream = await Task<Stream>.Factory.FromAsync(httpwebrequest.BeginGetRequestStream, httpwebrequest.EndGetRequestStream, null))
                {
                    await requestStream.WriteAsync(data, 0, data.Length);
                }

                response = await httpRequest(httpwebrequest);

            }
            catch (Exception ex)
            {

                return null;
            }

            return response;

        }

        public async Task<string> httpRequest(HttpWebRequest request)
        {
            string received;

            using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
            {
                using (var responseStream = response.GetResponseStream())
                {
                    using (var sr = new StreamReader(responseStream))
                    {

                        received = await sr.ReadToEndAsync();
                    }
                }
            }

            return received;
        }

我的疑问是:-

1) 如何将超时属性设置为 Httpwebrequest ??

1) How can i set timeout property to Httpwebrequest ??

2)我可以通过哪些不同的方式在我的 Windows Phone 8 应用程序中设置超时属性??

2)What are the different ways in which i can set the timeout property in my windows phone 8 app ??

请告诉我.

提前致谢.

推荐答案

您不能在 Windows Phone 上使用 HttpWebRequest.Timeout,因为该平台不存在它.

You can't use HttpWebRequest.Timeout on Windows Phone because it doesn't exist for that platform.

如果您愿意使用测试版库,则可以安装 HttpClient 通过 NuGet 并使用 its Timeout 属性.

If you're open to using a beta library, you could install HttpClient via NuGet and use its Timeout property.

否则,您可能最好使用 TaskEx.Delay,它是 Microsoft.Bcl.Async.安装该库后,您将替换这一行:

Otherwise, you're probably best off to use TaskEx.Delay, which is part of Microsoft.Bcl.Async. After installing that library, you would replace this line:

response = await httpRequest(httpwebrequest);

这样:

var httpTask = httpRequest(httpwebrequest);
var completeTask = await TaskEx.WhenAny(httpTask, TaskEx.Delay(5000));
if (completeTask == httpTask)
  return await httpTask;
else
  return null; // timeout

这篇关于如何在 Windows Phone 8 应用程序中为 httpwebrequest 设置超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆