C# - Json POST 请求已发送但未被 PHP 服务器接收

2023-10-12php开发问题
16

本文介绍了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 服务器接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17