Strange behavior of WWW on mobile platforms

Hi all,

Sorry for such a long post but my hope is to give as much info because I think I am running into a bug :stuck_out_tongue:

When I try to do two WWW post request everything works fine on my machine but as soon as I run it on a iOS or Android device things start to go a bit haywire. On Android I am always getting http response codes of 204 for my second request and on iOS things can take so long I get a timeout (for 4.3.3 only). I have verified that my server is running correctly as best I can (CURL, running the desktop client, and wireshark) and I am kind of stuck at this point. Here is the data I have collected so far:

Code I am running (note this is a base example, not the actual code):

public delegate void OnComplete(object data, string error);
public class Request : MonoBehaviour
{
public OnComplete cb;
public string URL;
public static void Send(string url, Dictionary<string,object> args, HttpResults type, OnComplete cb)
{
var newGO = new GameObject(“http_post”);
DontDestroyOnLoad(newGO);
Request post = newGO.AddComponent(“Post”) as Post;
post.URL = url;
post.Args = args;
post.Type = type;
post.cb = (object data, string error) =>
{
cb(data, error);
UnityEngine.Object.Destroy(newGO);
};
}

void Start ()
{
WWWForm form = new WWWForm();

if(null != this.Args this.Args.Count > 0)
{
string json = Json.Serialize(this.Args);
form.AddField(“json”, json);
}

WWW www = new WWW(URL, form);
StartCoroutine(WaitForResponse(www));
this.mWWW = www;
}

protected IEnumerator WaitForResponse(WWW www)
{
yield return www;
object data = null;
string header = “”;
foreach(KeyValuePair<string, string> entry in www.responseHeaders)
{
header += "Header: " + entry.Key + " = " + entry.Value + “\n”;
}
UnityEngine.Debug.Log(“www results:\n” +
"error = " + www.error +
"\nisDone = " + www.isDone +
"\nbytesDownloaded = " + www.bytesDownloaded +
"\nprogress = " + www.progress +
"\nupload progress = " + www.uploadProgress +
"\nURL = " + www.url +
“\nHeader = \n” + header +
"\nText = " + www.text
);
if(null == www.error)
{
data = Json.Deserialize(www.text); // Custom Json parser. You can remove this line if testing
}
cb(data, www.error);
}
} // end of class Request

I will make one call to the “Send” method and when the returns successfully I will make a second call. In my case I am talking to the same server each time and I have even tried hitting the same URL multiple times with the same results.

Device: MacBookPro
os: OSX Mavericks
Unity3D Version: 4.3.0 - 4.3.3
Connection type: Wired
Description: Works fine

Device: iPhone 5s
os: iOS 7
Unity3D Version: 4.3.3
Connection type: Wifi, local network with little to no additional devices on it
Description:

In this case the first request takes a LONG time to actually send the request to the server. Timeout happens and everything goes wrong from there.

Device: iPhone 5s
os: iOS 7
Unity3D Version: 4.3.2
Connection type: Wifi, local network with little to no additional devices on it
Description: Works fine

www results:
error =
isDone = True
bytesDownloaded = 308
progress = 1
upload progress = 1
URL = http://MYSERVER:MYPORT/URL
Header =
Header: CONTENT-ENCODING = utf-8
Header: SERVER = Cowboy
Header: CONTENT-LENGTH = 308
Header: CONNECTION = keep-alive
Header: DATE = Fri, 24 Jan 2014 06:02:40 GMT

Text = MYJSON_FROM_SERVER

Device: Google Nexus 5
os: Android 4.4 (KitKat)
Unit3D Version 4.3.3
Connection type: Wifi, local network with little to no additional devices on it
Description:

The first request will go through quickly but any additional WWW based sends will have a return code of 204. I KNOW my server is returning the valid headers and this works fine on MacOSX and curl based requests

Output from logcat

www results:
error =
sDone = True
bytesDownloaded = 0
progress = 0
upload progress = 1
URL = http://MYSERVER:MYPORT/URL
Header =
Header: NULL = HTTP/1.1 204 No Content
Header: CONNECTION = keep-alive
Header: CONTENT-LENGTH = 0
Header: DATE = Fri, 24 Jan 2014 05:58:32 GMT
Header: SERVER = Cowboy
Header: X-ANDROID-RECEIVED-MILLIS = 1390543109715
Header: X-ANDROID-RESPONSE-SOURCE = NETWORK 204
Header: X-ANDROID-SELECTED-TRANSPORT = http/1.1
Header: X-ANDROID-SENT-MILLIS = 1390543109711

Text =

Device: Google Nexus 5
os: Android 4.4 (KitKat)
Unit3D Version 4.3.2
Connection type: Wifi, local network with little to no additional devices on it
Description:

Same as 4.3.3

There is a WWW bug on 4.3.3 which is due to be fixed very very soon.

Hi Graham,

Would that be the reason my iOS 7 app keeps crashing back to the home screen?

My environment: Unity 4.3.4 + Xcode 5.0.1

If you have 4.3.4 then no. What logs get written by Unity?

Hey I’m currently using 4.3.4 and I still encounter this kind of bug. the www.text results becomes like this…

<link rel="apple-touch-icon-precomposed" sizes="72x72" href="http://media.cdn.s <p>On the web player and editor, it works fine.</p> <p>Please fix this bug, I need to launch my game asap. If not, is there an alternative way to get the www text data properly?</p> <p>edit: I’m using this for Android. On iOS it works fine.</p>

I ended up using HttpWebRequest. It works great on Android. =]

Too bad I can’t use www class. I hope this gets fixed soon.

cjimison you can try using this too if you want.