Search Unity

UnityWebRequest "Unknown Error" from a specific computer

Discussion in 'Multiplayer' started by Adrien-Ninpo, Feb 28, 2019.

  1. Adrien-Ninpo

    Adrien-Ninpo

    Joined:
    Oct 2, 2018
    Posts:
    17
    Hi,

    We are working on two different computers with Unity 2018.3. We're testing our web service by having the Unity editor send requests to a localhost sever; however, on one of these computers Unity Editor is unable to connect properly. Debugging `UnityWebRequest.error` only says "Unknown error".

    - Unity on this computer is able to properly acces an online version of the server (just by changing the URL)
    - Web browsers and other apps like Postman are able to properly access the local server, so we know the server is actually working.
    - If we stop the server. Unity throws a timeout error instead, so we know Unity is able to reach the server in some way.

    The servers are run from Visual Studio 2017 with the Net .core SDK 2.2
    On the surface, both machines look like the servers are configured in the same way, but I'm really not an expert about these; I do believe there has to be a difference between both servers, but I'm unable to figure it out.

    Where should I be looking ?

    I tried testing the connexion with the simplest setup I could find, an empty scene with only this script running. Tested both on 2018.3.0f2 and 2018.3.6f1

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class Test : MonoBehaviour {
    7.     static readonly string url = "https://localhost:44369/api/values";
    8.     void Start()
    9.     {
    10.         StartCoroutine(GetText());
    11.     }
    12.  
    13.     IEnumerator GetText()
    14.     {
    15.         using (UnityWebRequest request = UnityWebRequest.Get(url))
    16.         {
    17.             yield return request.SendWebRequest();
    18.  
    19.             if (request.isNetworkError) // Error
    20.             {
    21.                 Debug.Log(request.error);
    22.             }
    23.             else // Success
    24.             {
    25.                 Debug.Log(request.downloadHandler.text);
    26.             }
    27.         }
    28.     }
    29. }
    30.  
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    My first guess would be problems with HTTPS.
    Since it's your server, can you try pure HTTP and see if it works?
    If HTTP works, you can try HTTPS with custom certificate handler to see if it's CA issue (though that would be strange, since the error should be more descripting for that).
     
  3. Adrien-Ninpo

    Adrien-Ninpo

    Joined:
    Oct 2, 2018
    Posts:
    17
    Thanks for your reply.

    Pure HTTP doesn't seem to work at all : On both computers, neither Unity, Postman or web browsers are able to reach through, It just hangs for a long while, then fail.
    On Unity the error message is "Failed to receive data".
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    That would mean the server is HTTPS only.
    Have you tried to attach the custom certificate handler to UWR?
    Alternative is to try .NET networking APIs, whether those can reach the server. Strange that web browser can reach the server though.
     
  5. Adrien-Ninpo

    Adrien-Ninpo

    Joined:
    Oct 2, 2018
    Posts:
    17
    We actually never had to manipulate a UWR directly. Instead we used a plug-in SimpleHTTP which handles everything under the hood, I boiled the issue down to UWR while debugging.

    In the meantime I figured how to disable SSL completely; this made http work, and https unwork completely, but at least Unity can reach through now. The local server is only for internal testing, so not having this kind of security shouldn't be an issue.
     
    Meltdown likes this.
  6. navodayasachi

    navodayasachi

    Joined:
    May 28, 2019
    Posts:
    2
    Hi, I am facing the same issue.
    Tried using SimpleHTTP as suggested but this issue not resolved. I still see there is Unknown Error.
    calling API using SimpleHTTP plug-in (not to use UWR) is what I did. Please suggest if I am missing something here.
     
    AminAghajoon likes this.
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Is this error very occasional when using SimpleHTTP or easily reproducible?
     
  8. navodayasachi

    navodayasachi

    Joined:
    May 28, 2019
    Posts:
    2
    I see this error everytime, Yes easily reproducible.
     
  9. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Great, could you report a bug with a simple repro project?
    Thanks.
     
  10. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20
    Same here, I'm using UnityWebRequest with HTTP and still the error occurs. With Postman everything is fine, so the problem is related to Unity.
     
  11. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Can you show the code.
     
  12. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20
    Code (CSharp):
    1.  
    2.             Dictionary<string, string> header = new Dictionary<string, string>();
    3.             header.Add("Content-Type", "application/json");
    4.            
    5.             UnityWebRequest request = new UnityWebRequest();
    6.  
    7.             foreach (KeyValuePair<string, string> keyValuePair in header)
    8.             {
    9.                 request.SetRequestHeader(keyValuePair.Key,keyValuePair.Value);
    10.             }
    11.  
    12.             request.downloadHandler = new DownloadHandlerBuffer();
    13.             request.certificateHandler = new AcceptAnyCertifacteHandler();
    14.  
    15.             if (string.IsNullOrEmpty(postDataTableJsonString))
    16.             {
    17.                 request.url = url;
    18.                 request.method = "GET";
    19.             }
    20.             else
    21.             {
    22.                 byte[] bodyRaw = Encoding.GetBytes(postDataTableJsonString);
    23.                 request.uploadHandler = new UploadHandlerRaw(bodyRaw);
    24.                 request.url = url;
    25.                 request.method = "POST";
    26.             }
    27.  
    28.             if (Verbos)
    29.                 Debug.Log($"Sending Request to url : {url} with method {request.method} and headers : \n {JsonConvert.SerializeObject(header)}  \n data: {postDataTableJsonString}");
    30.  
    31.  
    32.             yield return request.SendWebRequest();
     
  13. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Can't spot anything wrong quickly.
    Have you tried using some HTTP debugging proxy like Fiddler, Charles etc.? I think you need to examine full traffic and check what are the differences between Unity and Postman.
     
  14. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20
    No, but I will check that and post the results here.
    Thanks
     
  15. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20

    It seems that UnityWebRequest failed to redirect to the correct endpoint. My server redirects HTTP traffic to HTTPS.
    - the final request with postman is fine (don't mind 404 error code).
     
  16. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Does that same request work when used directly, no redirect?
    What OS is on that machine and is certificate trusted?
     
  17. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20
    No, But the strange thing is that my HTTP proxy app cannot monitor it, I'm working on that with fiddler to see that if it can monitor that specific api.

    I'm working on Windows 10 pro machine, the certificate is self-signed but trusted on my machine. In addition to this, I have used
    Code (CSharp):
    1. AcceptAnyCertifacteHandler
    class to connect to my server. Also, I have to mention that other requests(with different api route) works and some api routes sometimes fail with "Unknown Error".
     
  18. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    HTTP proxies don't work when connection is to localhost or 127.0.0.1. They do work with computer name though.
    Try debugging if certificate handler is executed at all. It is only executed when certificate is not trusted, but otherwise valid and entire TLS works correctly.
     
  19. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20
    Certificate handler is not executed but I'm still getting the error "Unable to complete SSL connection"
     
  20. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Which version of TLS do you use?
    Does the same endpoint works fine when using web browser?
     
  21. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20
    How can I find that?
    yes it works.
     
  22. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Your browser should be able to provide such info (at least by clicking a "lock" icon in address bar in Firefox I can get details on TLS version and certificates used).
     
  23. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20
    TLS 1.2
     
  24. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Is you server accessible from outside?
    This looks like a bug, but we need to be able to debug under exactly the same conditions.
     
  25. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20
    Currently not, But I will give you the server address when I have deployed that.
     
  26. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Great, thanks.
    Once you can, report a bug with simple repro project and provide a case number here.
     
    AminAghajoon likes this.
  27. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,091
    Having same issue, tried using Net HttpWebRequest and got more verbose error:
    Code (CSharp):
    1. TlsException: Handshake failed - error code: UNITYTLS_INTERNAL_ERROR, verify result: UNITYTLS_X509VERIFY_FLAG_NOT_TRUSTED
     
  28. ScottPeal

    ScottPeal

    Joined:
    Jan 14, 2013
    Posts:
    61
    Having the same issue on Unity 2018.3.6.

    Running my ASP.NET Core 3 API code on the localhost in Visual Studios 2019. Postman works fine to the local host running the TLS cert issued by Visual Studios for HTTPS. CORS for any origin is active on the API.

    Other than having to deploy my API to a server, is there a work around? This approach would greatly slow my cycle time for development of the API and Unity Client.

    string url = "https://localhost:44375/api/User/Login";
    string json = @"{""username"": ""Ali"", ""password"": ""Ali@123""}";

    var request = new UnityWebRequest(url, "POST");
    byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes(json);
    request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
    request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
    request.SetRequestHeader("Content-Type", "application/json");
    yield return request.SendWebRequest();
     
    Last edited: Oct 31, 2019
  29. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20
    I have switched back to
    HTTP
    for my local development.
     
    ScottPeal likes this.
  30. ScottPeal

    ScottPeal

    Joined:
    Jan 14, 2013
    Posts:
    61
    I got it to work. Thanks for the excellent advice!

    Hey Unity Admins, if you are reading this, we really need better error messaging for network errors.

    VR Architect
     
    Last edited: Oct 31, 2019
  31. LisFox7

    LisFox7

    Joined:
    May 5, 2019
    Posts:
    11
    I have the same problem.
    In the rest API editor, requests work fine, both locally on http and on a remote server over https (TLS 1.2).
    From under Android when trying to connect to a remote server https (TLS 1.2) gives Unknown Error.
    This negates my efforts, I have already lost more than 1 week of time, the issue is not resolved.
    Previously, I used WCF it worked fine in the editor and in Android under mono, but since that would place the apk in google play, you need to use 64bit, and this can be done on IL2CPP, and under IL2CPP WCF C library System.ServiceModel.dll is not compiled, in the end, I can use WSDL or WCF only in mono, and if mono does not support ARM64, in the end I had to go to API rest requests, rewritten the server part, and eventually under Android Unknown Error, I spent a lot of time, but there is no solution, I'm confused, a vicious circle. Please consider ARM64-enabled web services, otherwise it's just sad.
     
  32. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Do you get this error in all Mono/Il2cpp 32/64 bit builds?
    This could be a bug.
     
  33. LisFox7

    LisFox7

    Joined:
    May 5, 2019
    Posts:
    11
    Rest API запросы на https (TLS 1.2) в любой сборке mono/ IL2CPP/64 bit даёт ошибку Unknown Error. Сейчас переделываю на сервере доступ по открытому каналу http, попробую подключиться без сертификата. Результат отпишу
     
  34. LisFox7

    LisFox7

    Joined:
    May 5, 2019
    Posts:
    11
    Rest API requests for https (TLS 1.2) in any mono/ IL2CPP/64 bit Assembly gives an Unknown Error. Now I alter on the server access on the open channel http, I will try to be connected without the certificate. Result I will unsubscribe
     
  35. LisFox7

    LisFox7

    Joined:
    May 5, 2019
    Posts:
    11
    On usual http worked perfectly. Therefore, we cannot use a certified connection. I when tested on https (TLS 1.2) I had the certificate not self-signed, and certified by the supplier, i.e. the most excellent. Anyway, the main thing though as that to work, I'll continue to use http. Thanks AminAghajoon for the hint.
     
  36. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Please report a bug for this.
     
  37. LisFox7

    LisFox7

    Joined:
    May 5, 2019
    Posts:
    11
    I don't know where to write about the problem, I just found this post when I was trying to solve my problem
    Your specialists can easily recreate the problem if they make an https connection on the server (perhaps a self-signed certificate)
    and try to request data:
    public static IEnumerator SendApi(ResponseRestApi sendInfo)
    {
    using (UnityWebRequest www = UnityWebRequest.Get(sendInfo.url + "?bytesInput=" + sendInfo.SendData))
    {
    yield return www.SendWebRequest();
    while (!www.isDone) // if you need to wait for an asynchronous call
    yield return true;

    if (www.isNetworkError || www.isHttpError) // || www.isHttpError
    {
    sendInfo.result = www.error;
    }
    else
    {
    if (www.isDone)
    {
    string jsonResult = System.Text.Encoding.UTF8.GetString(www.downloadHandler.data);
    sendInfo.result = jsonResult;
    }
    }
    }
    }

    public class ResponseRestApi
    {
    public ObscuredString url = "";

    public ObscuredString SendData = "";

    public ObscuredString result = "";
    }

    From under the unity editor everything will work fine, but from under the android (MONO, IL2CPP (32/64bin)) there will always be an Unknown Error.
    If the connection is transferred to http, then everything will work fine. But it is not a good practice to transmit data over an encrypted channel.
     
    carldevelopsforcoffee likes this.
  38. LisFox7

    LisFox7

    Joined:
    May 5, 2019
    Posts:
    11
    If you use the WCF (WSDL) library System.ServiceModel.dll and that work under MONO, and try to build a project under IL2CPP then at compile time will Exception and the compiler will close, ie IL2CPP is not possible to use WCF (WSDL) only rest API
     
  39. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Wait, which version of Unity are you using?
     
  40. LisFox7

    LisFox7

    Joined:
    May 5, 2019
    Posts:
    11
    This problem was tested on Unity 2019.1.9f1 (64-bit) and Unity 2019.2.12f1 (64-bit) Visual Studio 2017
     
  41. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    If you are using self-signed certificate, you have to attach certificate handler to UnityWebRequest and manually handle trust, or add the certificate to certificate store on device.
    If, however, it does work fine in Editor, but not on Android, please report a bug using Unity bug reporter.
     
  42. LisFox7

    LisFox7

    Joined:
    May 5, 2019
    Posts:
    11
    I did not use a self-signed certificate, but a certified one by the supplier, i.e. fully working, even with such a certificate, the problem exists. I do not know how to use Unity bug reporter, when the application is running on an Android emulator BlueStacks, unknown Error I get from the message www.error outputting it to Text.text
     
  43. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    You always report bug via Unity Editor. But you have to first try actual Android device, not an emulator. We don't officially support emulators.
     
    Joe-Censored likes this.
  44. LisFox7

    LisFox7

    Joined:
    May 5, 2019
    Posts:
    11
    You were right. I created two connections on the server one http, the other https. Made a test program with two buttons with the output of the result in the text field UI. And at start on BlueStacks and Nox emulators only http connection worked, for https gave an error "Unknown Error". But when I put the apk on my cell phone Xaomi Mi 9, earned both links and http and https. This is the first time when the work of the emulator and the real device are different for me.
     
  45. LisFox7

    LisFox7

    Joined:
    May 5, 2019
    Posts:
    11
    Thank you for your help
     
  46. WILEz1975

    WILEz1975

    Joined:
    Mar 23, 2013
    Posts:
    375
    I have the same problem.
    I make a request from Unity (I tried both, with the obsolete WWW and with UnityWebRequest.Get(url);) I have no return text.

    I think, my code is correct:

    Code (CSharp):
    1. // (in a coroutine)
    2. UnityWebRequest hs_get = UnityWebRequest.Get(url);
    3. yield return hs_get.SendWebRequest();
    4. mytext = hs_get.downloadHandler.text;

    "mytext" is always empty.
    But if I run the URL from a browser, the php page works and gives me the text that I need.
    Is it possible that my php server does not accept requests from Unity for some reason?
    PHP Version 5.4.16


    [EDIT]
    It doesn't depend on PHP, I also tried with a simple html page.
    Like "http://www.artistic-minds.it/test1.html"
    If the page is on my server it doesn't work.
    It works if for test I put the "google.com" URL.
     
    Last edited: Jan 28, 2020
  47. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Maybe server doesn't support HTTP-100 continue or chunked transfer. Try setting both of these properties to false on UWR (chunked transfer defaults to false in recent versions).
     
  48. WILEz1975

    WILEz1975

    Joined:
    Mar 23, 2013
    Posts:
    375
    I tried, but nothing has changed.
    This is my code.
    If I try with urls like "facebook.com" or "google.com", everything works.

    Code (CSharp):
    1.  
    2.  
    3. IEnumerator GetWebData() {
    4.  
    5.         string url = "https://www.artistic-minds.it";
    6.  
    7.       // UnityWebRequest hs_get = UnityWebRequest.Get(url); //old try
    8.  
    9.         UnityWebRequest hs_get = new UnityWebRequest(url);
    10.         hs_get.chunkedTransfer = false;
    11.         hs_get.useHttpContinue = false;
    12.      
    13.  
    14.         yield return hs_get.SendWebRequest(); // waiting for an answer
    15.  
    16.        /* Also tried this
    17.         while (!hs_get.isDone)
    18.         {
    19.             yield return null;
    20.         }
    21.       */
    22.  
    23.        // yield return hs_get;   //Also tried this
    24.  
    25.         // string pageText = Regex.Replace(hs_get.downloadHandler.text, "<.*?>", String.Empty); //Also tried this
    26.  
    27.         string pageText = hs_get.downloadHandler.text;
    28.  
    29.         print("<color=#FFFF00>WorldLeader request URL</color> " + url);
    30.         print("<color=#FFFF00>Return Text</color> " + pageText); //Always empty
    31.  
    32.  
    33.         if (hs_get.error != null)
    34.         {
    35.  
    36.             print("<color=#FF5500>There was an error getting the high score -</color> " + hs_get.error); // return "Unknow Error"
    37.         }
    38.         else
    39.         {
    40.             PrintOnText(pageText.Trim()); //My following function...
    41.         }
    42.     }
     
    Last edited: Jan 28, 2020
  49. WILEz1975

    WILEz1975

    Joined:
    Mar 23, 2013
    Posts:
    375
    I "solved" it using WebClient().

    Code (CSharp):
    1.  
    2. using (WebClient webClient = new WebClient())
    3.         {
    4.             webClient.Encoding = Encoding.UTF8;
    5.             hs_get = webClient.DownloadString(url);
    6.         }
     
    jencleary likes this.
  50. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    That's weird.
    The only thing I can spot is that the URL you've provided uses HTTP, but when I try to open it in browser, it gets upgraded to HTTPS. Maybe that's the reason?
    Try using https url with UWR and see if that work.
    And I think it would be a bug, if upgrade from HTTP to HTTPS doesn't work.