C# - Json的POST请求发送,但不是由PHP服务器接收 [英] C# - Json POST request sent but not received by PHP server
问题描述
我送从C#应用windowsform托管于OpenShift(红帽)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信息在日志中看到
<被beeing收到LI>但没有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".
这里是服务器上的日志行:
Here is the log line on the server :
< STRONG>这里是Wireshark中行:
Here is the line in 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
。请参见 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屋!