UnityWebRequest Android: java.io.IOException: unexpected end of stream on Connection

Hello guys,

I have a project where I communicate with some server over UnityWebRequest. In the editor everything works fine, but when I build it for Android and run it on my device I get an IOException sometimes while trying to send a web request. The exact error is as follows:

I create the request through:

UnityWebRequest.Post(url, data)

where data is my dictionary with data (and url is the url of course). For sending I use

public static IEnumerator SendRequestAsync(UnityWebRequest webRequest) {
    // Don't chunk the data
    webRequest.chunkedTransfer = false;

    // Add API key to the header
    webRequest.SetRequestHeader(API_KEY_HEADER_NAME, API_KEY);

    // Send the request
    yield return webRequest.SendWebRequest();
    // From here on webRequest.isNetworkError is true when the error happend

    // Some more processing of the request
}

If the error appears, then after my yield (line 9) the property isNetworkError of UnityWebRequest is set to true. I found some references to this kind of IOException, where people said they solved it by setting keep-alive to true (in native Android mostly), but it seems like UnityWebRequest doesn’t support this. If I create a new request to the same url and with the same data after the error, the request runs fine. Still I get the error often and I would love to know why it happens and how I maybe can prevent it.
Thank you guys in advance!

Regards,
Michael

Is that all information that you can see in logcat?
Also, it would help if I could see more of the code that does the request, preferably with the data and all headers that you set.

Yes this is all information to this error in my logcat. The function that constructs the request and calls the function mentioned above is as follows:

public void CreateGameRequest(string endPoint, GameData gameData, string userToken) {
    m_dataDictionary.Clear();
    m_dataDictionary["gamegridID"] = gameData.GridID.ToString();
    m_dataDictionary["x"] = gameData.row.ToString();
    m_dataDictionary["y"] = gameData.column.ToString();
    m_dataDictionary["history"] = gameData.history;
    UnityWebRequest webRequest = UnityWebRequest.Post(URL + endPoint, m_dataDictionary);
    webRequest.SetRequestHeader("token", userToken);
    StartCoroutine(SendRequestAsync(webRequest));
}

GameData is just some simple class wrapping some primitive values:

public class GameData {
    public int GridID = -1;
    public int row = -1;
    public int column = -1;
    public string history = "";
}

And the issue only happens when you run on Android? Never in editor?

No I never had it in editor, but on at least two different Android devices. It doesn’t seem to happen on iOS either.

Sounds worth of a bug report.

I get the same error too:

java.io.IOException: unexpected end of stream on Connection{91.121.33.92:80, proxy=DIRECT@ hostAddress=91.121.33.92 cipherSuite=none protocol=http/1.1} (recycle count=0)

It only shows on android device (never on editor). I have a simple PUT method.
I noticed that when the PUT body is more than 1000 characters length, the error appears on android device and If body length is lass than 700 chars, the error never appears on android device. however It wont show on editor anyway at any circumstances.

I also noticed that despite the error the server gets the complete PUT body! I mean even when android device throw the error, the server has the complete body and responds correctly.

here is my code:

void PUT(string strCorrectRequest, string body, Action<int, string> pmCallback)
    {
        UnityWebRequest www = UnityWebRequest.Put(strCorrectRequest, body);
        www.timeout = 20;

        www.SetRequestHeader("Content-Type", "application/json");
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.responseCode.ToString());
            Debug.Log(www.error);
            pmCallback((int)www.responseCode, www.error + "\nReqUrl: " + strCorrectRequest + "\n");
        }
        else
        {
            Debug.Log(www.responseCode.ToString());
            Debug.Log(www.downloadHandler.text);

             try
             {
              pmCallback((int)www.responseCode, www.downloadHandler.text);
             }
             catch (Exception ex)
             {
              Debug.LogError("PUT Call Back Error : " + ex.Message + "\nReqUrl: " + strCorrectRequest + "\n" + ex.StackTrace);
             }
        }
    }

I just found the solution: if you set www.chunkedTransfer = true; It works fine! hurraa! :slight_smile:

1 Like