2
我在C#的客戶,我有以下代碼:問題發送POST變量與C#客戶端
Uri uri = new Uri(@"http://myserver/test.php");
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Method = WebRequestMethods.Http.Post;
//request.ContentType = "application/json";
string req = "er3=12";
Console.WriteLine("Req: " + req);
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
byte[] byteData = encoder.GetBytes(req);
request.ContentLength = byteData.Length;
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
這是調用PHP測試頁面(即將休止服務代替):
<?php
echo "post vars:<br>";
foreach ($_POST as $key => $value) {
echo "$key -> $value<br>";
}
echo "end post vars:<br>";
?>
我的問題是,當我運行應用程序,在我得到"post vars:<br>end post vars:<br>"
的響應,所以變量er3沒有收到。
如果我運行一個簡單的html表單,post變量IS被正確讀取。
C#代碼中可能出錯或丟失了什麼?
感謝
我看不到您在HTTP標頭中指定編碼的位置。 PHP頁面是否需要ASCII編碼? – 2011-06-02 15:32:26