WP8将JSON发送到服务器

时间:2013-09-14 22:13:29

标签: json post windows-phone-8

我正在尝试将一些JSON数据发布到我的服务器。我使用的简单代码来自:Http Post for Windows Phone 8

代码:

        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);
        }

我在awaitTask上收到错误:

enter image description here

有人看到明显的错误吗?

1 个答案:

答案 0 :(得分:0)

通过将方法签名更改为:

来解决此问题

public async Task ReportSighting(瞄准目击)