如何将c#windows应用程序中的json字符串发布到php页面?
我使用以下代码,但它从php页面返回空字符串?
string Uname, pwd, postData, postData1;
Uname = txtUname.EditValue.ToString();
pwd = txtPassword.EditValue.ToString();
List<request1> JSlist = new List<request1>();
request1 obj = new request1();
obj.emailid = Uname;
obj.password = pwd;
JSlist.Add(obj);
JavaScriptSerializer serializer = new JavaScriptSerializer();
string s;
s = serializer.Serialize(obj);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://lab.amusedcloud.com/test/login_action.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(s);
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
MessageBox.Show(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
MessageBox.Show(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
PHP
<?php
$json_array = json_decode($_POST['json']);
?>
连接成功建立,但此php页面返回array(0){}
答案 0 :(得分:0)
基于ContentType属性,您应该对发送到PHP脚本的内容进行表单编码。如果是这种情况,您需要确保您发送的内容是键/值格式:
[key]=[value]
在您的情况下,看起来PHP脚本需要一个带有“json”键的参数,因此您发送的数据将类似于:
"json=" & s
希望有所帮助。