使用 Windows Phone App 传递 POST 变量
本文关键字:POST 变量 传递 App Windows Phone 使用 | 更新日期: 2023-09-27 18:32:58
我正在尝试使用php脚本将数据从Windows Phone应用程序插入数据库。这是我在单击按钮时执行的 C# 代码。
var request = WebRequest.Create(new Uri("xxx/Insert.php")) as HttpWebRequest;
request.Method = "POST";
string postdata = "Title=test&CategoryID=1";
byte[] data = Encoding.UTF8.GetBytes(postdata);
using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
{
await requestStream.WriteAsync(data, 0, data.Length);
}
我在 php 脚本中获取变量
<?php
$title = $_POST["Title"];
$categoryID = $_POST["CategoryID"];
...
?>
有人在这里遇到了同样的问题,但解决方案不起作用,因为1.) 网络客户端不适用于 WP82.) 第二个解决方案在 app.i.g.cs 行 global::System.Diagnostics.Debugger.Break() 处抛出异常
问题根本就是什么都没有发生。有没有人遇到过同样的问题?
我使用System.Net.Http.HttpClient和System.Net.Http.FormUrlEncodedContent解决了这个问题。
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("baseUrl.net");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("Title", "test")
});
var result = await client.PostAsync("/insert.php", content);
string resultContent = "result: "+result.Content.ReadAsStringAsync().Result;
编辑:等待客户端的后异步