【发布时间】:2018-02-12 02:25:43
【问题描述】:
我正在从 C# 客户端向 PHP 服务器发送 JSON 消息。
C#客户端代码如下:
private String baseUrl = null;
private String clientId = null;
private static readonly HttpClient client = new HttpClient();
private Task<string> Send(string method, Dictionary<string, string> metadata)
{
ServicePointManager.ServerCertificateValidationCallback = new
RemoteCertificateValidationCallback
(
delegate { return true; }
);
String jsonRequest = "{\"method\":\"checkLicence\"}";
StringContent content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("CLIENT_ID", clientId);
String clientSecret = encodeClientSecret(clientId);
client.DefaultRequestHeaders.Add("CLIENT_SECRET", clientSecret);
var result = client.PostAsync(baseUrl + "/api", content);
HttpResponseMessage response = result.Result;
return response.Content.ReadAsStringAsync();
}
在服务器端,PHP/Symfony 端点如下:
/**
* @Route("/api", name="api")
*/
public function api(Request $request) {
$this->checkAuth();
$params = array();
$content = $request->getContent();
}
通过PHP调试发现收到这个请求时$content变量为empty。为什么不包含 JSON 字符串?
编辑:为了更清楚地说明这一点 - 使用 Wireshark 我可以观察到我的 PHP 服务器似乎接受了原始 POST 请求,并以 302(移动)响应进行响应,然后以某种方式重定向(现在是 GET)请求到达我的/api 端点,并且没有内容(因为它是重定向?)
【问题讨论】:
-
你的
Request的属性是什么? -
对不起,你能说得更具体点吗?
-
是
Request在你的PHP代码中是用户定义的类吗? -
@JericCruz
Request指的是 Symfony 类Symfony\Component\HttpFoundation\Request