博世智能家居 API C# RestRequest 错误 "The request was aborted: Could not create SSL/TLS secure channel."

Bosch Smart Home API C# RestRequest Error "The request was aborted: Could not create SSL/TLS secure channel."

我正在尝试使用 RestSharp 呼叫本地博世智能家居 API。 Bosch 的文档展示了如何使用 Postman 来 HTTP 请求本地 API,所有功能都正常。在创建自签名证书并相应地设置 Postman 之后,我什至可以请求设备列表。所以我尝试开发一个简单的 C# 代码来通过 RestSharp 请求相同的列表。

static void RestGet()
        {
            System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            const String WEBSERVICE_URL = "https://10.20.1.41:8444/smarthome/devices";
            const string certificate_path = @"C:\Users\niko\Documents\certificates\certificate.pfx";
            const string certificate_pass = "....";
            const string systemPassword = "...."; //encrypted in BASE64
            string ua = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";

            ServicePointManager.Expect100Continue = true;
            ServicePointManager.DefaultConnectionLimit = 9999;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;


            var client = new RestClient("https://10.20.1.41:8444/smarthome/devices");
            client.Timeout = 100000;
            X509Certificate2 cer = new X509Certificate2(certificate_path, certificate_pass);
            client.ClientCertificates = new X509CertificateCollection() { cer };
            var request = new RestRequest(Method.GET);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("api-version", "1.0");
            request.AddHeader("Systempassword", systemPassword);
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.ErrorMessage);
            Console.WriteLine(response.Content);
            Console.ReadKey();
        }

由于某种原因,服务器只发送错误消息:

The request was aborted: Could not create SSL/TLS secure channel.

我一整天都在想办法弄明白,但由于 API 很新,所以还没有任何关于它的信息。

所以。一旦我注意到当 Fiddler 在后台打开时请求实际上会成功,我就知道我的错误必须是我如何将证书加载到我的程序中(因为 Fiddler 使用单独的证书,你必须提供)。注意到这一点后,我做的第一件事就是将我提供给 Fiddler 的证书复制到我的项目中,并将其加载到程序中。它终于起作用了!我无法告诉您该证书与其他证书有何不同。