使用Webclient上载字符串将JSON字符串上载到PHP

时间:2014-04-15 19:46:12

标签: c# windows-phone-7 windows-phone-8

下面有代码我希望与php服务器进行通信是: 不幸的是没有任何帖子到php端(数组或单个字符串)。 var_dump返回null

****添加到php阅读RAW帖子数据:**现在可以使用

$json = file_get_contents('php://input');
    $obj = json_decode($json);**

   private void webClient2_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
        MessageBox.Show(e.Result);
        MessageBox.Show("ss");
    }
    private void btn_soksok_Click(object sender, RoutedEventArgs e)
    {

        StudentName student2 = new StudentName
        {
            f = "Craig",
            l = "Playstead",
        };


       // string datax = JsonConvert.SerializeObject(student2);
        string datax = "asdf";
           Uri uri = new Uri("http://XXX.co/up.php");
           WebClient webClient2 = new WebClient();
        //webClient2.DownloadStringCompleted += webClient_DownloadStringCompleted;
           webClient2.UploadStringCompleted += webClient2_UploadStringCompleted;

          // webClient2.Headers["Content-Type"] = "application/json; charset=utf-8";
           //webClient2.Headers["Accept"] = "application/json";


           webClient2.UploadStringAsync(uri, "POST", datax);
           MessageBox.Show(datax);

    }

类对象

   public class StudentName {
        public string f { get; set; }
        public string l { get; set; }


    }

和php端

<?php
//header('Content-Type: application/json;charset=utf-8');
var_dump($_POST);
$x = json_decode($_POST[0]);
echo $x;
?>

1 个答案:

答案 0 :(得分:0)

这是您的代码所需要的

webClient2.Headers["Content-Type"] = "application/x-www-form-urlencoded";

然后

 string datax = JsonConvert.SerializeObject(r);
 datax = "0=" + datax;

在服务器中,我会将echo $x;替换为print_r($x);