UnityWebRequest returns "\u001f�\b"

UnityWebRequest returns "\u001f�\b"

我尝试将 POST 表单发送到我的 PHP 脚本,然后 return 输出。

它在 unity 2017 上工作,但自从我更新到 2018 后,我总是得到这个结果: "\u001f�\b"

就像你在下面的代码中看到的那样,我测试了 3 个不同的版本,所有版本 return 总是相同的结果

IEnumerator sendLogin()
{
    // Creating the form
    WWWForm loginUser = new WWWForm();
    loginUser.AddField("name", fieldUsername.text);
    loginUser.AddField("password", fieldPassword.text);

    //Version 0 - Result: "\u001f�\b"
    WWW www = new WWW(url, loginUser);
    yield return www;
    Debug.Log(www.text);

    // Version 1 - Result: "\u001f�\b"
    /*
    using (UnityWebRequest www = UnityWebRequest.Post(url, loginUser))
    {
        yield return www.SendWebRequest();
        Debug.Log(www.downloadHandler.text);
    }*/

    // Version 2 - Result: "\u001f�\b"
    /*UnityWebRequest download = UnityWebRequest.Post(url, loginUser);
    yield return download.SendWebRequest();
    Debug.Log(download.downloadHandler.text);
    */
}

我也试过添加.chunkedTransfer = false;

下面是我的 PHP 脚本,它可以在浏览器中正常运行(使用 "PostMan Google Chrome Extension"

进行了测试
<?php

$name = $_POST['name'];
$password = $_POST['password'];

if(!isset($name) || !isset($password))
{
    include('403.html');
    return;
}

$con=mysqli_connect("localhost","USERNAME","PASSWORD","DATABASE");

$name = stripslashes($name);
$password = stripslashes($password);
$name = mysqli_real_escape_string($con, $name);
$password = mysqli_real_escape_string($con, $password);
$password = md5($name . $password . $name);

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    return;
}

$sql= "SELECT password FROM Accounts WHERE name = '$name'";

if($result = mysqli_query($con, $sql))
{
    $rowcount = mysqli_num_rows($result);

    if($rowcount == 0)
    {
        echo ("A user with this name doesn't exists!");
    }
    else
    {
        while($row = mysqli_fetch_assoc($result))
        {
            if($password == $row['password'])
            {
                echo ("Logged In");
            }else
            {
                echo ("Wrong password!");
            }
        }
    }
}

mysqli_free_result($result);
mysqli_close($con);

?>

您似乎收到了压缩响应...

试试这个:

request.AutomaticDecompression = DecompressionMethods.GZip;

如果这有效,我找到了你的答案

很可能服务器正在返回压缩数据。可能是一个 GZIP 数据,它要求您 decompress WWW.bytes 结果而不是 WWW.text 然后使用 ASCII.GetString 从解压缩数据中获取字符串:

解压功能:

static byte[] Decompress(byte[] gzip)
{
    using (GZipStream stream = new GZipStream(new MemoryStream(gzip),
                                              CompressionMode.Decompress))
    {
        const int size = 4096;
        byte[] buffer = new byte[size];
        using (MemoryStream memory = new MemoryStream())
        {
            int count = 0;
            do
            {
                count = stream.Read(buffer, 0, size);
                if (count > 0)
                {
                    memory.Write(buffer, 0, count);
                }
            }
            while (count > 0);
            return memory.ToArray();
        }
    }
}

然后与您的网络请求代码一起使用:

IEnumerator sendLogin()
{
    // Creating the form
    WWWForm loginUser = new WWWForm();
    loginUser.AddField("name", fieldUsername.text);
    loginUser.AddField("password", fieldPassword.text);

    //Version 0 - Result: "\u001f�\b"
    WWW www = new WWW(url, loginUser);
    yield return www;
    //Debug.Log(www.text);

    //Decompress the data
    byte[] decompress = Decompress(www.bytes);
    //Convert to string
    string text = System.Text.ASCIIEncoding.ASCII.GetString(decompress);
    Debug.Log(text);
}