Search Unity

UnityWebRequest Unknown Error

Discussion in 'Scripting' started by adamt, Jul 21, 2016.

  1. adamt

    adamt

    Joined:
    Apr 1, 2014
    Posts:
    116
    Looking through my crash logs, I see a number of "Unknown Errors" coming back from UnityWebRequest in 5.3.5p8 when downloading AssetBundles on iOS. Has anyone else experienced this? Re-requesting the download seems to work just fine, but I get basically no context as to what's going on:

    webRequest.isError: true
    webRequest.error: Unknown Error
    webRequest.responseCode: -1001 (or sometimes -1005)


    Are these related to CFURL error codes? Looking here, I see that -1001 is kCFURLErrorTimedOut and -1005 is kCFURLErrorNetworkConnectionLost, so maybe there's a network hiccup that I need to recover from. However, looking through UnityWebRequest's documentation, I don't see a built-in way to handle that short of explicitly checking for those response codes and retrying the entire download (since at that point, I'm past the webRequest.Send() call).
     
  2. alvaro-em

    alvaro-em

    Joined:
    Feb 23, 2012
    Posts:
    77
    Hi, @adamt .

    I have been fighting with a very similar issue, for 2 days now and so far no luck. I am experiencing download failures in Android with "Unknown Error" in the error field. It only happens on Android, and only at the first attempt. A second attempt is always successful.

    I have noticed that it happens much more often when using my office wifi. The number of error is much lower (I am not even sure I have ever seen one) when I use my carrier connection. Based on similar problems on WebRequest class I have found on other forums, all I have learned is that it might be related to timeouts or proxy usages, but none of them are accesible using the UnityWebRequest class.

    Did you have any luck dealing with this, or did you find any clue that could put me on the right path? If so, I would be really pleased to read about what you found!

    Thank you very much in advance and kind regards!
     
  3. adamt

    adamt

    Joined:
    Apr 1, 2014
    Posts:
    116
    Sort of. I basically do a check for isError and if it's true, I retry the download. I don't really check for the specific response code and just assume that any error is potentially retryable/recoverable. The code is all in a coroutine so it looks something like this (for example, downloading the AssetBundle manifest):
    Code (CSharp):
    1. private IEnumerator DoDownload()
    2. {
    3.     string url = 'https://example.com';
    4.     int tryCount = 0;
    5.  
    6.     while(tryCount < 5)
    7.     {
    8.         using(UnityWebRequest webRequest = UnityWebRequest.GetAssetBundle(url))
    9.         {
    10.             yield return webRequest.Send();
    11.  
    12.             if(!webRequest.isError)
    13.             {
    14.                 AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequest);
    15.  
    16.                 if(bundle == null)
    17.                 {
    18.                     Debug.LogErrorFormat(string.Format("Could not load AssetBundle manifest. URL: {0}", url));
    19.                     tryCount++;
    20.                 }
    21.                 else
    22.                 {
    23.                     this.assetBundleManifest = bundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
    24.                     Log("AssetBundle manifest successfully downloaded. AssetBundle count: {0}", this.assetBundleManifest.GetAllAssetBundles().Length);
    25.  
    26.                     yield break;
    27.                 }
    28.             }
    29.             else
    30.             {
    31.                 tryCount++;
    32.                 Debug.LogWarningFormat("Could not download manifest{0}. Response code: {1}, Attempt #: {2}", (tryCount < RequestRetryCount ? ", retrying" : ""), webRequest.responseCode, tryCount);
    33.  
    34.                 if(tryCount < RequestRetryCount)
    35.                 {
    36.                     yield return new WaitForSeconds(1);
    37.                 }
    38.             }
    39.         }
    40.     }
    41. }
     
  4. alvaro-em

    alvaro-em

    Joined:
    Feb 23, 2012
    Posts:
    77
     
  5. alvaro-em

    alvaro-em

    Joined:
    Feb 23, 2012
    Posts:
    77
    Hi! I finally fixed the issue. My code was just fine. The problem was external. My network admin was using a load balancer, so from time to time, my external ip was changed. Every time my IP was changed I got this strange issue. Once my ip is fixed, my error was gone.

    Thank you, @adamt !