使用https访问受SSL保护的服务器?

时间:2019-07-05 11:37:18

标签: c# unity3d ssl https

我是客户端开发人员(Unity / C#),并且我具有在连接时需要SSL证书的API。我有此证书(.pfx)。 当我尝试通过HttpWebRequest类发出请求(下面的代码侦听)时,出现错误(代码400:错误的请求,未发送所需的SSL证书)。知道如何使它起作用吗?

当我将URL放在浏览器中时,我会弹出一个窗口,要求您提供证书,我可以在那里提供证书,然后得到结果!

也许我需要使用来自请求的流?

以下是请求在Inspector中的外观: enter image description here

void Connect() {
    // I think it is not necessary
    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);

    // certificate creation
    certificate = new X509Certificate2(path, pass); 

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    // adding certificate to request
    request.ClientCertificates.Add(certificate);
    request.Method = "GET";
    request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
    request.Accept = "application/json";

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}

public static bool ValidateServerCertificate(
        object sender,
        System.Security.Cryptography.X509Certificates.X509Certificate certificate,
        X509Chain chain,
        System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    return true;
}

0 个答案:

没有答案