gpt4 book ai didi

.net - 将 TLS 证书添加到异常为 : The request was aborted: Could not create SSL/TLS secure channel 的 API 请求

转载 作者:太空宇宙 更新时间:2023-11-03 14:50:12 25 4
gpt4 key购买 nike

为了能够使用某些 API,我必须使用 TLS 证书(在 1.1 版本中)。

我的代码如下:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://someapi/request/");

request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = Encoding.UTF8.GetByteCount(postData);
request.KeepAlive = false;

request.ProtocolVersion = HttpVersion.Version11;

ServicePointManager.Expect100Continue = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

X509Certificate2 certificate = new X509Certificate2(@"d:\TLScertificate.p12", "password");

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

try
{
store.Open(OpenFlags.ReadWrite);

if (!store.Certificates.Contains(certificate))
{
store.Add(certificate);
}

int indexOfCertificate = store.Certificates.IndexOf(certificate);
certificate = store.Certificates[indexOfCertificate];
}
finally
{
store.Close();
}

request.ClientCertificates.Add(certificate);
request.PreAuthenticate = true;

using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) // Exception
{
}

request.GetResponse() 期间,我总是遇到异常:请求被中止:无法创建 SSL/TLS 安全通道。

供应商回答我说:

There need be,

Root Ca v1 test.pem in your Truststore and TLSCertificate in your Keystore

请告诉我应该如何处理 .pem 文件?它应该添加到请求中,与 TLScertificate.p12 文件相同?当我向请求添加第二个 X509Certificate2(没有任何密码)时,我仍然遇到相同的错误。

最佳答案

首先,您可以立即对请求使用加载的证书

   X509Certificate2 certificate = new X509Certificate2(@"d:\TLScertificate.p12", "password");
request.ClientCertificates.Add(certificate);

pem 文件必须导入到您的计算机 KeyStoremmc -> 文件 -> 添加/删除管理单元 -> 证书

这有助于将 pem 转换为 crt Convert .pem to .crt and .key

关于.net - 将 TLS 证书添加到异常为 : The request was aborted: Could not create SSL/TLS secure channel 的 API 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35128325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com