我是 C# 的新手,所以我想知道是否有人可以帮助我解决这个问题。我正在尝试将 HttpPost 从 Windows Phone 8 发送到服务器。我发现了两个我想结合起来的例子。
第一个是发送 Http Post (http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx) 的示例。这个问题是 Windows Phone 8 不支持它。
第二个示例是使用 BeginGetResponse ( http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.net.httpwebrequest(v=vs.105).aspx )。这支持 Windows Phone 8。
我需要像第一个示例一样将第二个示例转换为 BeginGetRequestStream()。我会尝试自己解决这个问题,但如果有人已经知道如何做到这一点,我会在网上发布。我相信这会对其他 WP8 开发人员有所帮助。
更新 我现在正在尝试从服务器获得响应。我开始了一个新问题。请点击此链接 ( Http Post Get Response Error for Windows Phone 8 )
最佳答案
我目前还在处理一个 Windows Phone 8 项目,下面是我向服务器发布的方式。 Windows Phone 8 在某种程度上限制了对完整 .NET 功能的访问,我读过的大多数指南都说您需要使用所有功能的异步版本。
// server to POST to
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);
}
关于c# - 适用于 Windows Phone 8 的 HTTP 发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14698879/