0

I am trying to POST some JSON data to my Server. I am using simple code which I have attained from: Http Post for Windows Phone 8

Code:

        string url = "myserver.com/path/to/my/post";

        // HTTP web request
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "text/plain; charset=utf-8";
        httpWebRequest.Method = "POST";

        // Write the request Asynchronously 
        using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream,
                                                                 httpWebRequest.EndGetRequestStream, null))
        {
            //create some json string
            string json = "{ \"my\" : \"json\" }";

            // convert json to byte array
            byte[] jsonAsBytes = Encoding.UTF8.GetBytes(json);

            // Write the bytes to the stream
            await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
        }

I am getting an error though on await and Task:

enter image description here

Anyone see the obvious error?

Community
  • 1
  • 1
Subby
  • 5,120
  • 15
  • 68
  • 123
  • yes, look at the error messages. Did you decorate the method with the "async" word? – AFD Sep 14 '13 at 23:03
  • I changed the method signature to: [Async] public Task ReportSighting(Sighting sighting) But i get Async could not be found – Subby Sep 15 '13 at 00:44
  • async and not [A]sync, keywords are case sensitive in C# – Waleed Sep 15 '13 at 01:16

1 Answers1

0

Fixed it by changing method signature to:

public async Task ReportSighting(Sighting sighting)

Subby
  • 5,120
  • 15
  • 68
  • 123