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.
HttpClientinsteadHttpWebRequest. 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..