Search Unity

Question WebRequest sometimes does not return

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

  1. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    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;
    5.  
    6.             if (uwr.result == UnityWebRequest.Result.ProtocolError) {
    7.             Debug.LogFormat ("Error downloading file");
    8.         } else {
    9.             Debug.Log("Success!");
    10.         }
    11. }
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,065
    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

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    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

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    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

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,998
    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.