Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question WebRequest sometimes does not return

Discussion in 'Scripting' started by saifshk17, Mar 27, 2023.

  1. saifshk17


    Dec 4, 2016
    I am using Unity's webrequest. It works most of the times and I get a response with the data but sometimes it does not return the data at all. Is it because I am running it in a loop? The reason I am running it in a loop is because multiple URLs are getting called.

    I thought of adding a while loop since if the data is not received, it will call the request again but won't that degrade my performance? What is the suitable solution here?

    Code (CSharp):
    1. for (int i = 0; i < urls.Count; i++) {
    2.             UnityWebRequest webReq = UnityWebRequest.Get (urls[i]);
    3.             yield return webReq.SendWebRequest ();
    4.             var json = webReq.downloadHandler.text;
    6.             if (uwr.result == UnityWebRequest.Result.ProtocolError) {
    7.             Debug.LogFormat ("Error downloading file");
    8.         } else {
    9.             Debug.Log("Success!");
    10.         }
    11. }
  2. Adrian


    Apr 5, 2008
    Do you maybe disable or unload the script the coroutine is tied to? This will stop the coroutine and it will not be able process the result of the web request.

    Otherwise, try setting timeout to a small value. Web requests don't have a timeout by default and can hang indefinitely if the packet gets lost.
  3. Aurimas-Cernius


    Unity Technologies

    Jul 31, 2013
    Your code only checks for one type of error - the protocol one. The request is successful only if result is success, all other values are different types of errors.
  4. saifshk17


    Dec 4, 2016
    No i do not set my script to disable. What can be done to fix this?
    how do i make sure the webrequest runs again and completes it?
  5. Bunny83


    Oct 18, 2010
    This should be more specific. Your title suggests that your request get stuck and never return. However I have the feeling you mean that some of your requests error out and don't provide the data you want, but the request actually does finish. Is that correct?

    If that is correct, as @Aurimas-Cernius said, you should check for all potential errors. You usually look for the Success result and anything else is an error.

    There are many reasons why a webrequest may fail. It could be a transport issue (connection loss) though if you have a stable connection it's usually more likely that the server explicitly responded with an error. That may result in an actual error that is reflected in the "uwr.result", or it may be reflected in the data. That behaviour completely depends on the server.

    So first of all, make sure you actually handle errors correctly:

    Code (CSharp):
    1.     UnityWebRequest webReq = UnityWebRequest.Get (urls[i]);
    2.     yield return webReq.SendWebRequest ();
    3.     if (webReq.result != UnityWebRequest.Result.Success)
    4.     {
    5.         Debug.Log("Error downloading file. Result: " + uwr.result);
    6.     }
    7.     else
    8.     {
    9.         var data = webReq.downloadHandler.text;
    10.         Debug.Log("Success!\n" + data);
    11.         // use your data here
    12.     }
    This should tell you what kind of error you actually get and if the request is successful, it should show what data it received. Keep in mind that when your code throws an exception, that code is terminated and won't continue.

    ps: I just noticed in your code snippet you named your web request "webReq" but your condition contains "uwr". Is this your actual code? If so your error check does not check the actual request. Maybe it was just an editing issue here on the forum? As the code stands it doesn't make much sense.