1

I'm trying to do a WebRequest to a site from a Windows Phone Application. But is vry important for me to also get the response from the server. Here is my code:

 Uri requestUri = new Uri(string.Format("http://localhost:8099/hello/{0}", metodo)); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri); httpWebRequest.ContentType = "application/xml; charset=utf-8"; httpWebRequest.Method = "POST"; using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream, httpWebRequest.EndGetRequestStream, null)) { string xml = "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">Ahri</string>"; byte[] xmlAsBytes = Encoding.UTF8.GetBytes(xml); await stream.WriteAsync(xmlAsBytes, 0, xmlAsBytes.Length); } 

Unfortunatelly, I have no idea of how I could get the response from the server. Does anyone have an idea?

Thanks in advance.


Thanks to @max I found the solution and wanted to share it above. Here is how my code looks like:

 string xml = "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">Claor</string>"; Uri requestUri = new Uri(string.Format("http://localhost:8099/hello/{0}", metodo)); string responseFromServer = "no response"; HttpWebRequest httpWebRequest = HttpWebRequest.Create(requestUri) as HttpWebRequest; httpWebRequest.ContentType = "application/xml; charset=utf-8"; httpWebRequest.Method = "POST"; using (Stream requestStream = await httpWebRequest.GetRequestStreamAsync()) { byte[] xmlAsBytes = Encoding.UTF8.GetBytes(xml); await requestStream.WriteAsync(xmlAsBytes, 0, xmlAsBytes.Length); } WebResponse webResponse = await httpWebRequest.GetResponseAsync(); using (var reader = new StreamReader(webResponse.GetResponseStream())) { responseFromServer = reader.ReadToEnd(); } 

I hope it will help someone in the future.

2
  • We have no idea what your service does, how it looks like. How do you expect us to answer it? Commented Apr 14, 2015 at 20:46
  • 1
    I suggest you should use HttpClient instead HttpWebRequest. for that you might need to add an extra Nu-Get Package by Microsoft. It will make your web call easy. For further help, brief your question little more.. Commented Apr 15, 2015 at 4:43

1 Answer 1

2

This is very common question for people new in windows phone app development. There are several sites which gives tutorials for the same but I would want to give small answer here.

In windows phone 8 xaml/runtime you can do it by using HttpWebRequest or a WebClient.

Basically WebClient is a wraper around HttpWebRequest.

If you have a small request to make then user HttpWebRequest. It goes like this

HttpWebRequest request = HttpWebRequest.Create(requestURI) as HttpWebRequest; WebResponse response = await request.GetResponseAsync(); using (var reader = new StreamReader(response.GetResponseStream())) { string responseContent = reader.ReadToEnd(); // Do anything with you content. Convert it to xml, json or anything. } 

Although this is a get request and i see that you want to do a post request, you have to modify a few steps to achieve that.

Visit this place for post request.

If you want windows phone tutorials, you can go here. He writes awesome tuts.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @max. Your answer is perfect and solved my problem. You are a life savior!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.