Search Unity

UnityWebRequest and HTTPS not working

Discussion in 'Scripting' started by HarpSeal1, Jul 9, 2018.

  1. HarpSeal1

    HarpSeal1

    Joined:
    Oct 23, 2013
    Posts:
    35
    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

    Code (CSharp):
    1.  ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    2.         {
    3.             return true;
    4.         };
    right before I instantiate my unitywebrequest, but that does not help me any. Any ideas guys, im all out.
     
  2. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    348
    HarpSeal1 likes this.
  3. HarpSeal1

    HarpSeal1

    Joined:
    Oct 23, 2013
    Posts:
    35
    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.
     
    l_racher and castana1962 like this.
  4. castana1962

    castana1962

    Joined:
    Apr 10, 2013
    Posts:
    400
    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
     
  5. Bycob

    Bycob

    Joined:
    Jun 13, 2018
    Posts:
    1
    I'm currently working on 2017.4.5 LTS and the class CertificateHandler does not exist. Is there any alternative ?
     
  6. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    348
    the alternative is to use the native httpclient. Unitys webrequest doesnt support custom certs before 2018 versions.
     
  7. OlivierPons

    OlivierPons

    Joined:
    Sep 20, 2014
    Posts:
    25
    So you're saying:

    Code (CSharp):
    1. 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!
     
  8. YawJatah

    YawJatah

    Joined:
    Apr 11, 2015
    Posts:
    4
    arimacdigital likes this.
  9. arimacdigital

    arimacdigital

    Joined:
    Nov 27, 2017
    Posts:
    4

    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.

    Code (CSharp):
    1. using System;
    2. using UnityEngine.Networking;
    3. using System.Security.Cryptography.X509Certificates;
    4.  
    5. namespace PlayFab.Internal
    6. {
    7.     public class CustomCertificateHandler : CertificateHandler
    8.     {
    9.         // Encoded RSAPublicKey
    10.         private static readonly string PUB_KEY = "";
    11.  
    12.  
    13.         /// <summary>
    14.         /// Validate the Certificate Against the Amazon public Cert
    15.         /// </summary>
    16.         /// <param name="certificateData">Certifcate to validate</param>
    17.         /// <returns></returns>
    18.         protected override bool ValidateCertificate(byte[] certificateData)
    19.         {
    20.             return true;
    21.         }
    22.     }
    23. }
    24.  
    Also in any place we use a UnityWebRequest

    Code (CSharp):
    1. using System;
    2. CustomCertificateHandler  certHandler = new CustomCertificateHandler();
    3.  
    4. UnityWebRequest www = new UnityWebRequest("url", "POST/GET...");
    5. www.certificateHandler = certhndler();
    6.  
     
  10. Tom42_59

    Tom42_59

    Joined:
    Dec 16, 2019
    Posts:
    1
    Sorry for picking up an old thread, but the second part of your code doesnt match. You call
    Code (CSharp):
    1. certhndler()
    lick a method, but it isn't from my understandng. Please could you either fix it or explain
     
unityunity