【问题标题】:WP8 Sending JSON to ServerWP8 将 JSON 发送到服务器
【发布时间】:2013-09-15 09:03:56
【问题描述】:

我正在尝试将一些 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);
        }

我在await 和Task 上遇到错误:

有人看到明显的错误吗?

【问题讨论】:

  • 是的,看看错误信息。你有没有用“async”这个词装饰方法?
  • 我将方法签名更改为:[Async] public Task ReportSighting(Sighting Sighting) 但我得到 Async 找不到
  • async 而不是 [A]sync,C# 中的关键字区分大小写

标签: json post windows-phone-8


【解决方案1】:

通过将方法签名更改为:

公共异步任务 ReportSighting(Sighting Sighting)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多