dto5637 2013-06-29 12:00
浏览 36

将json数据从c#windows应用程序发布到php页面

How do I post a json string from c# windows application to a php page?

I am using the following code but it returns null string from php page?

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']);

?>

connection established successfully but this php page returns array(0){}

  • 写回答

1条回答 默认 最新

  • douzhi7082 2013-06-29 14:58
    关注

    Based on the ContentType property, it looks like you should be form encoding the content your sending to the PHP script. If thats the case you need to make sure the content you are sending is in key/value format:

    [key]=[value]
    

    In your case it looks like the PHP script is expecting a parameter with the key "json", so the data your sending would be something like:

    "json=" & s 
    

    Hope that helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用