UnityWebRequest SSL/TLS certificate validation?

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:

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;

When I try a simple “POST” I always get “Unknown Error” and responseCode == 0;

    IEnumerator Authentification()
    {
        var fu = @"{""data1"": ""fudata"",""data2"": ""fudata2""}";
        byte[] payload = new byte[fu.Length * sizeof(char)];

        System.Buffer.BlockCopy(fu.ToCharArray(),0,payload,0, payload.Length);

        UploadHandler upload = new UploadHandlerRaw(payload);
        upload.contentType = "application/json";
       
        UnityWebRequest request = new UnityWebRequest("https://someSide.com/PostRequest");
        request.uploadHandler = upload;

        yield return request.Send();

        if(request.isError)
        {
            Debug.Log(request.error + request.errorCode);
        }
        else
        {
            Debug.Log(request.downloadHandler.text);
        }
    }

If something like this is not possible, is there some kind of workaround?

1 Like

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.”

Some way to handle certificate validation would be handy… any word on this?

Same problem on my side. Any news about that?

Hello guys!
Check my plugin for Unity, working with promises is better!
GitHub - proyecto26/RestClient: 🦄 A Promise based REST and HTTP client for Unity 🎮 (Supported all platforms)
Also you can see the demo, it’s very easy!
Let me know what you think, Nicholls

Still searching for a solution to this issue…

Custom TLS certificate validation is supported since Unity 2018.1 (currently beta).