0

i've been reading here for quite a long time, but in this case i'm not getting any further. I'm rather new to Windows Phone development and facing the following problem.

I'm calling a webservice were I have to post a xml request message. I've got the code working in regular c# (see code below)

private static string WebRequestPostData(string url, string postData) { System.Net.WebRequest req = System.Net.WebRequest.Create(url); req.ContentType = "text/xml"; req.Method = "POST"; byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData); req.ContentLength = bytes.Length; using (Stream os = req.GetRequestStream()) { os.Write(bytes, 0, bytes.Length); } using (System.Net.WebResponse resp = req.GetResponse()) { if (resp == null) return null; using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream())) { return sr.ReadToEnd().Trim(); } } } 

But for Windows Phone (8) development it needs to be async. After searching the web, and trying the various samples given here I came to the following code:

private async void DoCallWS() { string url = "<my_url>"; // HTTP web request var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.ContentType = "text/xml"; httpWebRequest.Method = "POST"; // Write the request Asynchronously using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream, httpWebRequest.EndGetRequestStream, null)) { string requestXml = "<my_request_xml>"; // convert request to byte array byte[] requestAsBytes = Encoding.UTF8.GetBytes(requestXml); // Write the bytes to the stream await stream.WriteAsync(requestAsBytes , 0, requestAsBytes .Length); stream.Position = 0; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { //return reader.ReadToEnd(); string result = reader.ReadToEnd(); } } } 

The string result has the value of my request xml message i'm trying to sent....

I'm aware that async void methodes are not preferred but i will fix that later.

I've also tried to following the solution as described by Matthias Shapiro (http://matthiasshapiro.com/2012/12/10/window-8-win-phone-code-sharing-httpwebrequest-getresponseasync/) but that caused the code to crash

Please point me in the right direction :)

Thnx Frank

2
  • 1
    What exactly is your question? What is wrong with the code you're using. Commented Nov 30, 2014 at 19:26
  • I do not get a answer from the webservice. reader.ReadToEnd() has the value of my request parameter <my_request_xml> Commented Nov 30, 2014 at 20:18

1 Answer 1

3

What you're doing is only writing to the request stream. You're missing the code which reads from the response.

The reason you're getting back your request xml is that you reset the request stream and read from that exact stream.

Your method should look as follows:

private async Task DoCallWSAsync() { string url = "<my_url>"; // HTTP web request var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.ContentType = "text/xml"; httpWebRequest.Method = "POST"; // Write the request Asynchronously using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream, httpWebRequest.EndGetRequestStream, null)) { string requestXml = "<my_request_xml>"; // convert request to byte array byte[] requestAsBytes = Encoding.UTF8.GetBytes(requestXml); // Write the bytes to the stream await stream.WriteAsync(requestAsBytes , 0, requestAsBytes .Length); } using (WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(httpWebRequest.BeginGetResponse, httpWebRequest.EndGetResponse, httpWebRequest)) { var responseStream = responseObject.GetResponseStream(); var sr = new StreamReader(responseStream); string received = await sr.ReadToEndAsync(); return received; } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.