4

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.

user1516781
  • 973
  • 1
  • 20
  • 42

2 Answers2

9

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
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
0

You can use HttpStatusCode.HttpStatusCode is an enum which can be used to get the type of error in HttpWebRequest.

catch(WebException ex) {

HttpWebResponse response = (HttpWebResponse)ex.Response;

if(response.StatusCode==HttpStatusCode.GatewayTimeout) { }

}

The GatewayTimeout indicates that an intermediate proxy server timed out while waiting for a response from another proxy or the origin server.For more information you can refer to the msdn site for this.Hope it helps

user3207655
  • 188
  • 2
  • 15