【发布时间】: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