title: C# WP8 async HttpWebRequest Post with xml request message
body: <p>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.</p>
  
  <p>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)</p>
  
  <pre><code>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();
          }
      }
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>private async void DoCallWS()
  {
      string url = "&lt;my_url&gt;";
  
          // 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&lt;Stream&gt;(httpWebRequest.BeginGetRequestStream,
                                                                   httpWebRequest.EndGetRequestStream, null))
      {
          string requestXml = "&lt;my_request_xml&gt;";
  
          // 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();
          }
      }
  }
  </code></pre>
  
  <p>The string result has the value of my request xml message i'm trying to sent....</p>
  
  <p>I'm aware that async void methodes are not preferred but i will fix that later.</p>
  
  <p>I've also tried to following the solution as described by Matthias Shapiro (<a href="http://matthiasshapiro.com/2012/12/10/window-8-win-phone-code-sharing-httpwebrequest-getresponseasync/" rel="nofollow">http://matthiasshapiro.com/2012/12/10/window-8-win-phone-code-sharing-httpwebrequest-getresponseasync/</a>) but that caused the code to crash</p>
  
  <p>Please point me in the right direction :)</p>
  
  <p>Thnx Frank</p>