Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Timeout for httpwebrequest in windows phone 8 app?

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

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

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;
        }

My Doubt is :-

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

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

Please let me know .

Thanks in Advance.

like image 628
user1516781 Avatar asked Jan 14 '23 03:01

user1516781


1 Answers

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

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

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);

with this:

var httpTask = httpRequest(httpwebrequest);
var completeTask = await TaskEx.WhenAny(httpTask, TaskEx.Delay(5000));
if (completeTask == httpTask)
  return await httpTask;
else
  return null; // timeout
like image 113
Stephen Cleary Avatar answered Jan 26 '23 00:01

Stephen Cleary