2014-12-02 30 views
0

我想从Windows Phone应用程序插入数据到数据库使用PHP脚本。 这是我单击按钮时执行的C#代码。通过POST变量与Windows Phone应用程序

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"]; 
... 
?> 

有人有同样的问题here,但解决的办法没有奏效,因为 1)Web客户端不可用于WP8 2.)第二种解决方案在全局:: System.Diagnostics.Debugger.Break()

这一行中引发App.igcs中的异常。问题根本就没有发生。有没有人遇到同样的问题?

回答

0

我使用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; 

编辑:等待客户端的PostAsync

相关问题