C# 使用Windows Phone应用程序传递POST变量
我正在尝试使用php脚本将Windows Phone应用程序中的数据插入数据库。 这是我点击按钮时执行的C#代码C# 使用Windows Phone应用程序传递POST变量,c#,php,windows-phone-8,windows-phone,httpwebrequest,C#,Php,Windows Phone 8,Windows Phone,Httpwebrequest,我正在尝试使用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(post
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);
}
var request=WebRequest.Create(新Uri(“xxx/Insert.php”))作为HttpWebRequest;
request.Method=“POST”;
string postdata=“Title=test&CategoryID=1”;
byte[]data=Encoding.UTF8.GetBytes(postdata);
使用(var requestStream=await Task.Factory.fromsync(request.BeginGetRequestStream,request.EndGetRequestStream,request))
{
wait requestStream.WriteAsync(数据,0,数据.Length);
}
我在php脚本中使用
<?php
$title = $_POST["Title"];
$categoryID = $_POST["CategoryID"];
...
?>
有人也有同样的问题,但解决方案不起作用,因为
1.)WebClient不适用于WP8
2.)第二个解决方案在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;
使用(var-client=new-HttpClient())
{
client.BaseAddress=新Uri(“baseUrl.net”);
var content=newformurlencodedcontent(new[]
{
新的KeyValuePair(“标题”、“测试”)
});
var result=wait client.PostAsync(“/insert.php”,content);
字符串resultContent=“result:+result.Content.ReadAsStringAsync().result;
编辑:等待客户端的PostAsync