I am trying to call Tomcat Rest server via UnityWebRequest. When Tomcat is configured for HTTP everything its fine, but when i try to add SSL and HTTPS it seems to not work.
Tomcat has self signed certificate, and a redirect URI for HTTPS.
When i try to call over UnityWebRequest i get message: “Unknown Error” , and Status code: “0”.
First thing to do was to switch to Unity beta 2018.1.
There you have the UnityWebRequest.certificateHandler
This allows to set up custom certificate validation.
one last thing to do is to create an object extending CertificateHandler to manage Certificate validation.
(See here in Unity beta documentation)
Here is the code :
MyMonoBehaviour :
IEnumerator GetRequest(string uri){
UnityWebRequest request = UnityWebRequest.Get(uri);
request.certificateHandler = new AcceptAllCertificatesSignedWithASpecificKeyPublicKey();
yield return request.SendWebRequest ();
if (request.isNetworkError)
{
Debug.Log("Something went wrong, and returned error: " + request.error);
}
else
{
// Show results as text
Debug.Log(request.downloadHandler.text);
}
}
Thanks for this solution - it pointed me into the right direction. For any of you who also don’t like the fact that the pk string must be read from the debugger in order to be set correctly, here is a slightly more sophisticated solution. Warining however: Im not a cryptography expert…
For tomcat and java you can easily create self-signed certs using the keytool. You likely have created on, if you end up here… (use google if you need to know more about creating self signed certs with keytool).
You can (text-) exoprt a certificate using the keytool (as text) via:
This will give you a text file (mySelfSignedSSL.cert) containing the certificate data. No the fun part: Strip of the -----BEGIN CERTIFICATE-----
and -----END CERTIFICATE----- part and you’ve got a standart (base64 encoded) string representation of your cert including the public key. However, you can not use this string in the solution above for two reasons: First, it contains more data than the key bytes, second: the character encoding is different (so the same byte sequence will result in different characters).
Anyhow, you can readily perform an analogous test with this standard certificate representation - just like that:
You can write a simple parser for the complete cert file (simply remove first an last line) - this way you can easily distribute the complete certificate file with your game and use HTTPS / SSL self signed certs. If the server certificate changes, just ship a new .cert file…