c# - 适用于 Windows Phone 8 的 HTTP 发布

标签 c# http-post windows-phone

我是 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/

相关文章:

c# - 在类中的变量上应用自定义标识符

javascript - Angular.JS HTTP POST 数据未发送

php - HTTP Post 不向数据库发送数据?

javascript - 从下拉列表中更改不同的表(onChange)

c# - 为什么 System.Random.Sample 会抛出 IndexOutOfRangeException?

c# - 延期执行会不会先调用Dispose导致失败?

php - 发送POST请求: missing trailing slash in url triggers redirecting GET request

windows-phone-7 - WP7 : Is it possible to intercept the backstack before the journal thumbnail is created/stored

c# - 如何处理 Windows Phone 8.1 中的水平滑动事件?

在 Windows Phone 8 模拟器上找不到 SSL 证书