将json数据从c#windows应用程序发布到php页面
本文关键字:php 页面 应用程序 json 数据 c#windows | 更新日期: 2023-09-27 18:20:18
如何将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){}
基于ContentType属性,您应该对发送到PHP脚本的内容进行表单编码。如果是这种情况,你需要确保你发送的内容是密钥/值格式:
[key]=[value]
在您的情况下,PHP脚本似乎需要一个关键字为"json"的参数,因此您发送的数据将类似于:
"json=" & s
希望能有所帮助。