Search Unity

UnityWebRequest SSL/TLS certificate validation?

Discussion in 'Scripting' started by zlSimon, Jan 5, 2016.

  1. zlSimon

    zlSimon

    Joined:
    Apr 11, 2013
    Posts:
    31
    I am developing a WEBGL app which needs to interact with a REST API via https where the server certificate is untrusted. Since I cannot use System.Net on WebGL I tried to use the new UnityWebRequest but I found nothing where to handle certificates.
    In System.Net I would do something like this:
    Code (CSharp):
    1. ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
    When I try a simple "POST" I always get "Unknown Error" and responseCode == 0;

    Code (CSharp):
    1.     IEnumerator Authentification()
    2.     {
    3.         var fu = @"{""data1"": ""fudata"",""data2"": ""fudata2""}";
    4.         byte[] payload = new byte[fu.Length * sizeof(char)];
    5.  
    6.         System.Buffer.BlockCopy(fu.ToCharArray(),0,payload,0, payload.Length);
    7.  
    8.         UploadHandler upload = new UploadHandlerRaw(payload);
    9.         upload.contentType = "application/json";
    10.        
    11.         UnityWebRequest request = new UnityWebRequest("https://someSide.com/PostRequest");
    12.         request.uploadHandler = upload;
    13.  
    14.         yield return request.Send();
    15.  
    16.         if(request.isError)
    17.         {
    18.             Debug.Log(request.error + request.errorCode);
    19.         }
    20.         else
    21.         {
    22.             Debug.Log(request.downloadHandler.text);
    23.         }
    24.     }
    25.  
    If something like this is not possible, is there some kind of workaround?
     
    cogentEd likes this.
  2. cogentEd

    cogentEd

    Joined:
    Aug 3, 2015
    Posts:
    6
    I have the same problem. I'm so glad I found your post, @zlSimon. It helped me realize the https in my url was causing my "Unknown Error."
     
  3. a_tarerra

    a_tarerra

    Joined:
    Nov 7, 2012
    Posts:
    16
    Some way to handle certificate validation would be handy... any word on this?
     
  4. NicoL3AS

    NicoL3AS

    Joined:
    Oct 7, 2015
    Posts:
    18
    Same problem on my side. Any news about that?
     
  5. jdnichollsc

    jdnichollsc

    Joined:
    Sep 23, 2017
    Posts:
    9
    Hello guys!
    Check my plugin for Unity, working with promises is better!
    https://github.com/proyecto26/RestClient (Supported all platforms)
    Also you can see the demo, it’s very easy!
    Let me know what you think, Nicholls
     
  6. nilsubimax

    nilsubimax

    Joined:
    Apr 3, 2017
    Posts:
    17
    Still searching for a solution to this issue...
     
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Custom TLS certificate validation is supported since Unity 2018.1 (currently beta).