Http发布的Windows Phone 8
我是C#的新手,所以我想知道是否有人可以帮我解决这个问题。 我试图从Windows Phone 8发送HttpPost到服务器。 我find了两个我想合并的例子。
第一个是发送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发布获取Windows Phone 8的响应错误 )
我目前也在开发一个Windows Phone 8项目,这里是我如何发布到服务器。 Windows Phone 8对.NETfunction的访问权限有限,大部分指南都指出,您需要使用所有function的asynchronous版本。
// 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); }
我提出了一个更通用的asynchronous方法来支持成功和错误callback:
//Our generic success callback accepts a stream - to read whatever got sent back from server public delegate void RESTSuccessCallback(Stream stream); //the generic fail callback accepts a string - possible dynamic /hardcoded error/exception message from client side public delegate void RESTErrorCallback(String reason); public void post(Uri uri, Dictionary<String, String> post_params, Dictionary<String, String> extra_headers, RESTSuccessCallback success_callback, RESTErrorCallback error_callback) { HttpWebRequest request = WebRequest.CreateHttp(uri); //we could move the content-type into a function argument too. request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; //this might be helpful for APIs that require setting custom headers... if (extra_headers != null) foreach (String header in extra_headers.Keys) try { request.Headers[header] = extra_headers[header]; } catch (Exception) { } //we first obtain an input stream to which to write the body of the HTTP POST request.BeginGetRequestStream((IAsyncResult result) => { HttpWebRequest preq = result.AsyncState as HttpWebRequest; if (preq != null) { Stream postStream = preq.EndGetRequestStream(result); //allow for dynamic spec of post body StringBuilder postParamBuilder = new StringBuilder(); if (post_params != null) foreach (String key in post_params.Keys) postParamBuilder.Append(String.Format("{0}={1}&", key, post_params[key])); Byte[] byteArray = Encoding.UTF8.GetBytes(postParamBuilder.ToString()); //guess one could just accept a byte[] [via function argument] for arbitrary data types - images, audio,... postStream.Write(byteArray, 0, byteArray.Length); postStream.Close(); //we can then finalize the request... preq.BeginGetResponse((IAsyncResult final_result) => { HttpWebRequest req = final_result.AsyncState as HttpWebRequest; if (req != null) { try { //we call the success callback as long as we get a response stream WebResponse response = req.EndGetResponse(final_result); success_callback(response.GetResponseStream()); } catch (WebException e) { //otherwise call the error/failure callback error_callback(e.Message); return; } } }, preq); } }, request); }