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

查看:106
本文介绍了如何在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,因为该平台不存在HttpWebRequest.Timeout.

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

如果您愿意使用Beta库,则可以通过NuGet安装 HttpClient /a>并使用 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天全站免登陆