title: WP8 HttpWebRequest Post Not Working
body: <p>I have a Windows Phone Application and I I am trying to post data in JSON format to a WCF application. Although the connection is made, the server returns with a custom message with</p>
  
  <p>This is the C# code:</p>
  
  <pre><code>ReportSightingRequest.Instance.Source = Source.IPhone;
  var jsonData = JsonConvert.SerializeObject(ReportSightingRequest.Instance);
  var uri = new Uri("urlGoesHere", UriKind.Absolute);
  
  var request = (HttpWebRequest)WebRequest.Create(uri);
  request.Method = "POST";
  request.ContentType = "application/json";
  request.ContentLength = jsonData.Length;
  
  string received;
  using (var response = (HttpWebResponse)(await Task&lt;WebResponse&gt;.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
  {
      using (var responseStream = response.GetResponseStream())
      {
          using (var sr = new StreamReader(responseStream))
          {
              received = await sr.ReadToEndAsync();
          }
      }
  }
  </code></pre>
  
  <p>This is the WCF Interface:</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
  [Description("Description.")]
  Response.Response ReportSighting(ReportSightingRequest sighting);
  </code></pre>
  
  <p>This is the implementation:</p>
  
  <pre><code>public Response ReportSighting(ReportSightingRequest sightingRequest)
  {
      var response = new Response();
      if (sightingRequest == null || sightingRequest.TypeId == null)
      {
         response.Status = ResponseStatus.InvalidArguments;
         response.Message = "Request is null or no type has been supplied.";
         return response;
      }
  ...
  }
  </code></pre>
  
  <p>When I call the ReportSighting method form the phone, I get a "Request is null or no type has been supplied" message. The strange thing is that I <strong>AM</strong> sending a <code>TypeId</code> and the <code>sightingRequest</code> object on the WP8 side is definitely not null when i'm sending it. When I put a breakpoint on the jsonData, it has everything in it. The <code>ReportSightingRequest</code> object too is exactly the same as the <code>ReportSightingRequest</code> in the WCF application.</p>
  
  <p>It almost feels like that the object isn't being serialized. That's the only thing I can think of.</p>
  
  <p>Does anyone have any ideas/suggestions?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I've noticed that i'm actually not sending over the object. Shawn Kendrot's Answer seems to make sense but when I integrate his code, it returns with a <strong>Not Found</strong> error.</p>
  
  <p><strong>Update</strong>
  The following code works in a Console App:</p>
  
  <pre><code>        var jsonData = "a hard coded JSON string here";
          var uri = new Uri("a url goes here", UriKind.Absolute);
          var webRequest = (HttpWebRequest)WebRequest.Create(uri);
          webRequest.Method = "POST";
          webRequest.ContentType = "application/json; charset=utf-8";
          webRequest.ContentLength = jsonData.Length;
  
          webRequest.BeginGetRequestStream(ar =&gt;
          {
              try
              {
                  using (var os = webRequest.EndGetRequestStream(ar))
                  {
                      var postData = Encoding.UTF8.GetBytes(jsonData);
                      os.Write(postData, 0, postData.Length);
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
              }
  
              webRequest.BeginGetResponse(
                  ar2 =&gt;
                  {
                      try
                      {
                          using (var response = webRequest.EndGetResponse(ar2))
                          using (var reader = new StreamReader(response.GetResponseStream()))
                          {
                              var received = reader.ReadToEnd();
                              //Console.WriteLine(received);
                          }
                      }
                      catch (Exception ex)
                      {
                          Console.WriteLine(ex.Message);
                      }
                  }, null);
          }, null);
  </code></pre>
  
  <p><strong>Update</strong>
  I have changed my code in WP8 to match that of Shawn Kendrot's solution. The problem which I am facing here is that I get a <code>Not Found</code> error message:</p>
  
  <pre><code>webRequest.BeginGetRequestStream(ar =&gt;
              {
                  try
                  {
                      using (var os = webRequest.EndGetRequestStream(ar))
                      {
                          var postData = Encoding.UTF8.GetBytes(jsonData);
                          os.Write(postData, 0, postData.Length);
                      }
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show("Unsuccessful");
                  }
  
                  webRequest.BeginGetResponse(
                      ar2 =&gt;
                      {
                          try
                          {
                              using (var response = webRequest.EndGetResponse(ar2))
                              using (var reader = new StreamReader(response.GetResponseStream()))
                              {
                                  var received = reader.ReadToEnd();
                              }
                          }
                          catch (Exception ex)
                          {
                              MessageBox.Show("Unsuccessful");
                          }
                      }, null);
              }, null);
  </code></pre>
  
  <p>I get a:</p>
  
  <p>{System.UnauthorizedAccessException: Invalid cross-thread access.
     at MS.Internal.XcpImports.CheckThread()
     at MS.Internal.XcpImports.MessageBox_ShowCore(String messageBoxText, String caption, UInt32 type)
     at System.Windows.MessageBox.ShowCore(String messageBoxText, String caption, MessageBoxButton button)
     at System.Windows.MessageBox.Show(String messageBoxText)
     at Notify.Logic.WebServices.&lt;>c_<em>DisplayClass2.b</em>_1(IAsyncResult ar2)
     at System.Net.Browser.ClientHttpWebRequest.&lt;>c_<em>DisplayClass1d.b</em>_1b(Object state2)}</p>
  
  <p>When I try to do `MessageBox.Show(ex.Message);</p>
  
  <p><strong>Update</strong></p>
  
  <p>I have fixed the issue with the MessageBox.Show error message. </p>
  
  <p>The <code>webRequest.Headers</code> object has the following:</p>
  
  <p><code>{Content-Type: application/json; charset=utf-8;}</code></p>