1

I am sending a HTTP request from a C# windowsform application to PHP server hosted on OpenShift (Redhat). I am using the method POST, with Json data.

The problem is that :

  • the data seemed to be correctly sent (I see the packets in wireshark)
  • the php script is correctly launch and I see in the log that a POST message is received
  • but no POST data are beeing received ..

Here is the C# code :

string json = "{\"user\":\"test\"," +
                "\"n\":\"2\"}";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://........rhcloud.com/webservices.php");

request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = json.Length;

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    streamWriter.Write(json);
    streamWriter.Close();

    var httpResponse = (HttpWebResponse)request.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
         var result = streamReader.ReadToEnd();
         Debug.WriteLine("R : " + result);
    }
}

Here is the PHP code :

echo "Start Saving ! ";

// Handle Posted Data From C# App
if (isset($_POST) && !empty($_POST))
{
    echo 'Data Recieved';
}
else
{
  // Error
  echo 'No POST Data Found';
}   

The function always return : "Start Saving ! No POST Data Found".

Here is the log line on the server : https://dl.dropboxusercontent.com/u/14737942/log.png

Here is the line in wireshark : https://dl.dropboxusercontent.com/u/14737942/Wireshark_capture.png

Is someone seeing the problem? Do not hesitate to tell me if I am not clear. Could it be Openshift which intercept the data ? Does my php file have a problem?

1
  • You should make sure you post to exact same domain. I mean that you for example might post data to example.com and example.com makes redirection to www.example.com and then it won't work Commented Jul 28, 2014 at 22:20

1 Answer 1

4

PHP's $_POST does not understand JSON.

What you want is something along the lines of

// Error handling is left as an exercise
$input = json_decode(file_get_contents('php://input'), true);

You should then be able to use $input the way you seem to want to use $_POST. See json_decode for additional knobs to twiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much ! It totally fixed my problem :)
This save my life. In Android a simple $_Post will do, but in c# this line of code is the coolest line of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.