2014-12-02 33 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

相關問題