UnityWebRequest将空白数据发送到HTTP

时间:2018-02-28 03:43:34

标签: c# unity3d http-post unityscript

这是我将数据发送到主机的示例,但我认为它发送了空白数据。例如,在" serverKeycode"中,我发送"示例",但服务器只接收""。

C#

WWWForm form = new WWWForm();
form.AddField("serverKeycode", "example");
form.AddField("username", authUsername.text);
form.AddField("password", authPassword.text);

UnityWebRequest www = UnityWebRequest.Post("http://example.com/authenticate.php", form);
yield return www.SendWebRequest();

PHP

header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');

$server_key = "example";
$server_auth = filter_input(INPUT_POST, 'serverKeycode');
if ($server_auth == $server_key)
{
    // do something
}
else
{
    $myJSON->returnType = 2;
    $myJSON->message = "Key: " . $server_auth; // just for checking the key
}

从PHP返回JSON并在Unity中打印$message时,它什么都不打印。

这是否意味着Unity不发送数据?

它曾经在我的旧webhost上工作,如果我通过Postman发送POST请求,一切正常。我是否需要在防火墙中允许某些内容?

1 个答案:

答案 0 :(得分:2)

在致电yield return www.SendWebResquest()之前,只需添加www.chunkedTransfer = false;

即可