UnityWebRequest and HTTPS not working

Hey everyone,
I am trying to perform a unitywebrequest using an HTTPS link and its failing. The error text in my webrequest instance is blank. Using HTTP in the link and everything works as it should. My webserver has a self signed certificate and apparently this is a problem from research ive done, is this true? I’ve tried doing this like

 ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        };

right before I instantiate my unitywebrequest, but that does not help me any. Any ideas guys, im all out.

The servicepointmanager is used by c#'s native HTTPClient, to use a self signed cert with unitywebrequests you need this:

1 Like

Thanks nilsdr, that solved it! For those searching: subclass CertificateHandler and override ValidateCertificate to return true. Assign a new instance to the www’s certificateHandler property. Be aware that this is not safe for production and you should get a signed cert.

2 Likes

Hi HarpSeal1
Sorry to bother you but I am new in this topic but I am trying to perform a unitywebrequest to connect Unity and one Service Watson outside of SDK for Unity( named Machine Learning). Since you could use unitywebrequest to connect by any http, for it, Could you share the Class that do it? Would it be possible? Thanks for your time
Alejandro

I’m currently working on 2017.4.5 LTS and the class CertificateHandler does not exist. Is there any alternative ?

the alternative is to use the native httpclient. Unitys webrequest doesnt support custom certs before 2018 versions.

So you’re saying:

UnityWebRequest.Get("https://blablah.com")

will always fail? If no, do you have a link or a working sample?
I’ve been trying for 2 days to make my https webserver working, and now that it’s working, I’m just hoping that I’m wrong with UnityWebRequest!

Hi Guys, I had a similar issue with the latest update of Unity (2018.2.x). Using this thread and others around the thread I was able to come up with a solution. My solution is document on the PlayFab forum:

https://community.playfab.com/questions/25594/unable-to-complete-ssl-connection-since-unity-2018.html?childToView=25922#answer-25922

Basically I show how to implement your own certifcate handler.

hope this helps others that are stuck on this issue

1 Like

This solution worked for me pretty well!. Actually I omitted using the amazon pub key due to the fact that I’m using my application in a LAN. Therefore, I changed the return value of the Validate Certificate method to always be true. It’s highly not recommended for a public app though.

using System;
using UnityEngine.Networking;
using System.Security.Cryptography.X509Certificates;

namespace PlayFab.Internal
{
    public class CustomCertificateHandler : CertificateHandler
    {
        // Encoded RSAPublicKey
        private static readonly string PUB_KEY = "";


        /// <summary>
        /// Validate the Certificate Against the Amazon public Cert
        /// </summary>
        /// <param name="certificateData">Certifcate to validate</param>
        /// <returns></returns>
        protected override bool ValidateCertificate(byte[] certificateData)
        {
            return true;
        }
    }
}

Also in any place we use a UnityWebRequest

using System;
CustomCertificateHandler  certHandler = new CustomCertificateHandler();

UnityWebRequest www = new UnityWebRequest("url", "POST/GET...");
www.certificateHandler = certhndler();
2 Likes

Sorry for picking up an old thread, but the second part of your code doesnt match. You call certhndler() lick a method, but it isn’t from my understandng. Please could you either fix it or explain