HTTP POST的Windows Phone 8的 [英] Http Post for Windows Phone 8

查看:132
本文介绍了HTTP POST的Windows Phone 8的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C#,所以我在想,如果有人能帮助我在这。我想送HttpPost从Windows Phone 8的服务器。我发现了两个例子,我想结合。

I am new to C# so I was wondering if someone can help me out on this. I am trying to send HttpPost from Windows Phone 8 to the server. I found two examples that I would like to combine.

第一个是发送HTTP POST(<一的例子href=\"http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx\">http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx).与这一个问题是,它不是由Windows手机8.

The first one is an example of sending Http Post (http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx). The problem with this one is that it is not support by Windows Phone 8.

第二个例子是使用BeginGetResponse(<一href=\"http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.net.httpwebrequest(v=vs.105).aspx\">http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.net.httpwebrequest(v=vs.105).aspx).这支持的Windows Phone 8。

The second example is using the BeginGetResponse (http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.net.httpwebrequest(v=vs.105).aspx). This supports windows phone 8.

我需要第二示例转换成BeginGetRequestStream()之类的第一个例子。我会揣摩这一点我自己,但我在网上发布,如果有人已经知道如何做到这一点。我相信这将是其他WP8开发者有帮助。

I need to convert the second example into a BeginGetRequestStream() like the first example. I will try to figure out this myself, but I am posting online if someone already knows how to do this. I am sure this will be helpful for other WP8 developers.

更新
现在我想从服务器获得响应。我已经开始了一个新的问题。请点击此链接(<一个href=\"http://stackoverflow.com/questions/14703087/http-post-get-response-error-for-windows-phone-8\">Http帖子获得Windows Phone 8的响应错误)

推荐答案

我也是目前工作的一个的Windows Phone 8的项目,这里是我如何发布到服务器。的Windows Phone 8之类的具有完整的.NET能力有限,大部分导游,我读说你需要使用的所有功能异步版本。

I am also currently working on a Windows Phone 8 project and here is how I am posting to a server. Windows Phone 8 sort of has limited access to the full .NET capabilities and most guide I read say you need to be using the async versions of all the functions.

// 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);
}

这篇关于HTTP POST的Windows Phone 8的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆