Search code examples
c#httpunity-game-engineget-request

www.text returning strange value in Unity3D (C#)


I'm using Unity 3D's WWW to make http requests: http://docs.unity3d.com/ScriptReference/WWW.html

It seems that no matter what kind of data I'm trying to access, it just returns: ���� every time. I've tried json files, I've tried php that just generates a string. I can't seem to access the values on the server.

C#:

public string url = "http://www.onelittledesigner.com/data.php";

IEnumerator Start() {
    WWW www = new WWW(url);
    yield return www;

    if (!string.IsNullOrEmpty(www.error)) {
        Debug.Log(www.error);
    } else {
        Debug.Log(www.text);
    }

}

PHP:

<?php
  echo "textiness";
?>

Note: I've used www.texture successfully to pull images off of the server. However, www.text doesn't seem to be working.


Solution

  • Copying answer from comment, with some additional tests. My results are in the comments. Note that using Default, ASCII or UTF8 does work on my machine - it also should on yours.

        // returned from www.bytes, copied here for readability
        byte[] bytes=new byte[]{116, 101, 120, 116, 105, 110, 101, 115, 115};
    
        string customDecoded=""; 
        foreach(var b in bytes)
            customDecoded+=(char)b; 
    
        Debug.Log(customDecoded); // textiness
        Debug.Log(System.Text.Encoding.Default); // System.Text.ASCIIEncoding
        Debug.Log(System.Text.Encoding.Default.GetString(bytes));  // textiness
        Debug.Log(System.Text.Encoding.ASCII.GetString(bytes));  // textiness
        Debug.Log(System.Text.Encoding.Unicode.GetString(bytes)); // 整瑸湩獥
        Debug.Log(System.Text.Encoding.UTF7.GetString(bytes)); // textiness
        Debug.Log(System.Text.Encoding.UTF8.GetString(bytes)); // textiness
        Debug.Log(System.Text.Encoding.UTF32.GetString(bytes)); // 整湩
        Debug.Log(System.Text.Encoding.BigEndianUnicode.GetString(bytes)); // 瑥硴楮敳
    

    Please check if System.Text.Encoding.Default is ASCIIEncoding, maybe something changed the default value?