I use Unity Web Request in my project to send POST and GET requests to the server. It works correctly most of the time, but sometimes the response of the request is not right and when I check logs, I find out that the response of that request, is exactly response of the one before the latest response and because of that my project crash.
IEnumerator IUnityWebRequest(HTTPRequest request, Action<string> successCallback, Action<string> failCallback, bool hasLoading = true)
{
UnityWebRequest www = null;
if (request.requestType == HTTPRequestType.GET)
{
var parameters = FormatedParameters(request.parameters);
www = UnityWebRequest.Get(request.url + parameters);
}
else
{
www = new UnityWebRequest(request.url, UnityWebRequest.kHttpVerbPOST);
byte[] bytes = Encoding.UTF8.GetBytes(request.body);
www.uploadHandler = (UploadHandler)new UploadHandlerRaw(bytes);
www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
}
foreach (KeyValuePair<string, string> entry in request.headers)
www.SetRequestHeader(entry.Key, entry.Value);
www.timeout = timeOut;
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
if(www.error == "Request timeout")
{
failCallback("timeout");
}
else
{
failCallback(www.downloadHandler.text);
}
}
else
{
Debug.LogFormat("Response url: {0} | Message {1} ", www.url, www.downloadHandler.text);
successCallback(www.downloadHandler.text);
}
}