Search Unity

Question Unitytls error

Discussion in 'Scripting' started by bil_unity967, Apr 7, 2021.

  1. bil_unity967

    bil_unity967

    Joined:
    Feb 12, 2020
    Posts:
    6
    Hi all,

    I get an error in the console from time to time:
    curl error 56: Receiving data failed with unitytls error code 1048578

    I use UnityWebRequest to call a back-end. Most of the time it works but sometimes it gives me this error.
    My script will retry the call and it will get a restult back. But is there a way to better capture this? I do not like theses errors in console. And when the error happens. It takes a long time to make the call before it retries.

    this is how I make calls.
    api.url is the endpoint url
    data is a string (containing json object but stringified.)
    To post data instead of PUT but using a string (data) instead of a form I use the PUT webrequest but change the method to POST.

    Code (CSharp):
    1. protected IEnumerator ApiCall(string data, Action<string> successCallback = null, Action failCallback = null)
    2.         {
    3.             bool IsHttpError;
    4.             bool IsNetworkError;
    5.             int retries = 0;
    6.  
    7.  
    8.             do
    9.             {
    10.                 MonoBehaviour.print(data);
    11.  
    12.                 using UnityWebRequest www = UnityWebRequest.Put(api.url, data);
    13.                 www.method = "POST";
    14. #if UNITY_2017_2_OR_NEWER
    15.                 yield return www.SendWebRequest();
    16. #else
    17.             yield return request.Send();
    18. #endif
    19.  
    20. #if UNITY_2020_2_OR_NEWER
    21.                 IsHttpError = www.result == UnityWebRequest.Result.ProtocolError;
    22. #else
    23.                     IsHttpError = www.isHttpError;
    24. #endif
    25.  
    26. #if UNITY_2020_2_OR_NEWER
    27.                 IsNetworkError = www.result == UnityWebRequest.Result.ConnectionError;
    28. #else
    29.                     IsNetworkError = request.isNetworkError;
    30. #endif
    31.  
    32.                 if (IsHttpError || IsNetworkError || !www.isDone)
    33.                 {
    34.                     if (retries < 3)
    35.                     {
    36.                         yield return new WaitForSeconds(1f);
    37.                         retries++;
    38.                     }
    39.                     else
    40.                     {
    41.                         MonoBehaviour.print("Error...");
    42.                         MonoBehaviour.print(www.error.ToString());
    43.                         failCallback?.Invoke();
    44.                         break;
    45.                     }
    46.                 }
    47.                 else
    48.                 {
    49.                     MonoBehaviour.print(www.downloadHandler.text.ToString());
    50.                     successCallback?.Invoke(www.downloadHandler.text);
    51.                     break;
    52.                 }
    53.             } while (retries <= 3);
    54.         }
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,731
    Do you ever get a different error or is it always TLS receive error?
     
  3. bil_unity967

    bil_unity967

    Joined:
    Feb 12, 2020
    Posts:
    6
    It is only this TLS error.
     
  4. bil_unity967

    bil_unity967

    Joined:
    Feb 12, 2020
    Posts:
    6
    I changed the code to use POST from the start instead of the method = POST trick. But no luck. Same issue.

    Here is the modified code:

    Code (CSharp):
    1. protected IEnumerator ApiCall(string data, Action<string> successCallback = null, Action failCallback = null)
    2.         {
    3.             bool IsHttpError;
    4.             bool IsNetworkError;
    5.             int retries = 0;
    6.  
    7.  
    8.             do
    9.             {
    10.                 MonoBehaviour.print(data);
    11.                 UnityWebRequest www = new UnityWebRequest(api.url, "POST");
    12.                 byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(data);
    13.                 www.uploadHandler = new UploadHandlerRaw(jsonToSend);
    14.                 www.downloadHandler = new DownloadHandlerBuffer();
    15.                 www.SetRequestHeader("Content-Type", "application/json");
    16.  
    17. #if UNITY_2017_2_OR_NEWER
    18.                 yield return www.SendWebRequest();
    19. #if UNITY_2020_2_OR_NEWER
    20.                 IsHttpError = www.result == UnityWebRequest.Result.ProtocolError;
    21.                 IsNetworkError = www.result == UnityWebRequest.Result.ConnectionError;
    22. #else
    23.                 IsHttpError = www.isHttpError;
    24.                 IsNetworkError = request.isNetworkError;
    25. #endif
    26. #else
    27.                 yield return request.Send();
    28.                 IsHttpError = www.isError;
    29.                 IsNetworkError = www.isError;
    30. #endif
    31.  
    32.  
    33.  
    34.                 if (IsHttpError || IsNetworkError || !www.isDone)
    35.                 {
    36.                     if (retries < 3)
    37.                     {
    38.                         yield return new WaitForSeconds(1f);
    39.                         retries++;
    40.                     }
    41.                     else
    42.                     {
    43.                         MonoBehaviour.print("Error...");
    44.                         MonoBehaviour.print(www.error.ToString());
    45.                         failCallback?.Invoke();
    46.                         break;
    47.                     }
    48.                 }
    49.                 else
    50.                 {
    51.                     MonoBehaviour.print(www.downloadHandler.text.ToString());
    52.                     successCallback?.Invoke(www.downloadHandler.text);
    53.                     break;
    54.                 }
    55.             } while (retries <= 3);
    56.         }
     
  5. bil_unity967

    bil_unity967

    Joined:
    Feb 12, 2020
    Posts:
    6
    Oh, I have opted out in privacy settings (id.unity) and also disabled editor analytics (I'm using pro).
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,731
    Is the server something we can access? It would be nice to get a bug report for this so we could look, though that will require for us to be able to perform the same requests.