2
votes

HttpWebRequest in Windows phone 8

I am developing a windows phone 8 app using c#/xaml. I am facing some issues with httpwebrequest class. I want to download data from server using post method.But httpwebrequest is not working as expected. It is returning error (The remote server returned an error: NotFound) when i try to call webservices subsequently. What could be the reason?. Please help...Following is my code.

 string response = "";                 
 httpwebrequest = WebRequest.Create(new Uri(serviceurl)) as HttpWebRequest;
 httpwebrequest.Method = "POST";

            httpwebrequest.ContentType = "application/json";
            byte[] data = Serialization.SerializeData(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);


            var result = Serialization.Deserialize<T>(response);
            return result;


    }

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

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

                response.Close();

            }

            return received;
        }
        catch(Exception ex)
        {
            return "";
        }
    }
1
This basically means an HTTP 404. Have you checked that your serviceurl is correct? Try it in your browser, it should not give you a 404 - Kenneth
serviceurl is correct. still getting the same error.how can we handle multiple webservice request? i want to call multiple request in my application. - Sachin123
Well, the issue is still the same. You probably can't reach the server . Are you trying this on the phone or on the emulator? You could try to open a browser on either and put the URL in. - Kenneth
I'am trying this on device (lumia 920 and lumia 620).Both simulator and device have the same issue. - Sachin123

1 Answers

0
votes

Have you checked the inner exception? Because i had a similar problem an when i checked the inner exception i found that i got another error from the server. Check also the real http status code into the response header.