加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Windows Phone 8的Http文章

发布时间:2020-12-15 17:44:50 所属栏目:百科 来源:网络整理
导读:我是C#的新手,所以我想知道有人能帮助我吗?我试图将HttpPost从 Windows Phone 8发送到服务器.我发现两个我想结合的例子. 第一个是发送Http Post(http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx)的例子.这
我是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,jsonAsBytes.Length);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读