2

I try to send a POST form to my PHP script and then return the output.

It was working on unity 2017, but since I updated to 2018 I always get this result: "\u001f�\b"

Like you see in the code below I tested 3 different version, all of them return always the same result

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);
    */
}

I also tried to add .chunkedTransfer = false;

Below you see my PHP script, which works in the browser like it should (Tested it with "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);

?>

Programmer
  • 121,791
  • 22
  • 236
  • 328
Sascha
  • 63
  • 8
  • Possible duplicate of [Strange HttpClient result](https://stackoverflow.com/questions/36939980/strange-httpclient-result) – Leo Bartkus Oct 05 '18 at 17:55

2 Answers2

2

It's very likely that the server is returning a compressed data. Likely a GZIP data which requires that you decompress the WWW.bytes result instead of WWW.text then use ASCII.GetString to obtain the string from the decompressed data:

The decompression function:

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();
        }
    }
}

Then to use with your web request code:

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);
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • How do you know if this is compressed / or not (I'm working with both development server = not compressed, and prod = compressed)? – Olivier Pons Feb 22 '19 at 07:06
0

it looks like your getting a zipped response...

try this:

request.AutomaticDecompression = DecompressionMethods.GZip;

if this works i found your answer here

Technivorous
  • 1,682
  • 2
  • 16
  • 22
  • Sadly theres no .AutomaticDecompression variable BUT the answer below works, thanks anyway :D – Sascha Oct 05 '18 at 18:11