我正在从 C# Windows 表单应用程序向 OpenShift (Red Hat) 上托管的 PHP 服务器发送 HTTP 请求。我正在使用 POST 方法和 Json 数据。
问题是:
- 数据似乎已正确发送(我在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 数据”。
Here is the log line on the server :
Here is the line in wireshark :
有人看到问题了吗?如果我不清楚,请随时告诉我。
难道是Openshift拦截了数据?我的php文件有问题吗?
PHP's $_POST
不理解 JSON。
你想要的是类似的东西
// Error handling is left as an exercise
$input = json_decode(file_get_contents('php://input'), true);
然后你应该能够使用$input
你似乎想要使用的方式$_POST
. See json_decode用于转动额外的旋钮。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)