C# - Json POST 请求已发送但未被 PHP 服务器接收 [英] C# - Json POST request sent but not received by PHP server

查看:44
本文介绍了C# - Json POST 请求已发送但未被 PHP 服务器接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 C# windowsform 应用程序向托管在 OpenShift (Redhat) 上的 PHP 服务器发送 HTTP 请求.我正在使用 POST 方法和 Json 数据.

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.

问题在于:

  • 数据似乎正确发送(我看到了wireshark中的数据包)
  • php 脚本正确启动,我在日志中看到收到了一条 POST 消息
  • 但是没有收到 POST 数据..

这里是 C# 代码:

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

这是PHP代码:

echo "Start Saving ! ";

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

函数总是返回:开始保存!没有找到POST数据".

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

这是服务器上的日志行:

这是wireshark中的一行:

有人看到问题了吗?如果我不清楚,请随时告诉我.会不会是 Openshift 拦截了数据?我的php文件有问题吗?

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?

推荐答案

PHP 的 $_POST 不理解 JSON.

PHP's $_POST does not understand JSON.

你想要的是类似的东西

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

然后您应该能够像使用 $_POST 那样使用 $input.请参阅 json_decode 以了解其他要旋转的旋钮.

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.

这篇关于C# - Json POST 请求已发送但未被 PHP 服务器接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆