Question

There is code below i would like to achieve to communicate wth php server is: Unfortunately there is nothing posting to php side (array or single string). var_dump returns null

****added to php to read RAW post data :**now it works

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

code

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

    }

class object

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


    }

and php side

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

La solution

This is what you need in your code

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

and then

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

in the server I would replace echo $x; with print_r($x);

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top