gpt4 book ai didi

c# - 无法发送 HTTP 请求,因为请求已中止。 "Could not create SSL/TLS secure channel"

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

我目前正在开发一个 C# 网络应用程序,它使用 Invoice Ninja 的 API 来检查付款/发票信息。我已经设法使用 HttpClient 在我的本地机器上运行它,但是每当它被部署到部署服务器(Windows Azure VM)时,我都会收到以下错误:

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

启用和未启用 SSL 的站点的错误是相同的(开发站点没有,而实时站点有)。

我试过使用以下解决方案:

添加 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 |安全协议(protocol)类型.Tls12 |安全协议(protocol)类型.Tls11 | SecurityProtocolType.Tls; 在创建 HttpClient 之前。

使用

WebRequestHandler handler = new WebRequestHandler(); 
handler.ServerCertificateCustomValidationCallback += (sender, certificate, chain, errors) => true;
using (HttpClient client = new HttpClient(handler)) {
//Code goes here
}

手动将证书添加到 WebRequestHandler
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection collection = store.Certificates;

所有其他 HTTP 调用(Twilio、Authy、SendGrid)我一直在按预期在应用程序内部进行工作,但调用 Invoice Ninja 让我感到难过。

我不完全确定从这里到哪里去,任何帮助将不胜感激。

编辑:我制作了一个简单的控制台应用程序来检查是否是 IIS 干扰了 Http 调用,但不幸的是,同样的事情仍然发生。我仍然收到“请求已中止:无法创建 SSL/TLS 安全通道”的消息。错误。

这可能是某种服务器配置问题吗?

编辑 2:我尝试在不同的虚拟机上运行控制台测试应用程序,它在那里正常运行。我更加不确定从这里到哪里去。

这是我试过的代码,以防万一。

public static async Task<string> CallInvoiceNinja()
{
var resultString = string.Empty;

try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;

WebRequestHandler handler = new WebRequestHandler();
using (HttpClient client = new HttpClient(handler))
{
client.BaseAddress = new Uri("https://app.invoiceninja.com");
client.DefaultRequestHeaders.Add("X-Ninja-Token", "[TOKEN]");

var result = await client.GetAsync("/api/v1/payments");
resultString = await result.Content.ReadAsStringAsync();
}
}
catch(Exception ex)
{
resultString = ex.Message;
if(ex.InnerException != null)
{
resultString += "\n" + ex.InnerException.Message;
}
}

return resultString;
}

最佳答案

看来我看问题的方式不对。

我做了更多的探索,并尝试在服务器上的 IE 中打开其 API 的 Swagger 文档,发现这是由于我们的服务器没有 Invoice Ninja 所需的必要密码套件造成的,因为我们的服务器显然是在使用自定义的密码套件列表。

我添加了 API 使用的密码套件,并重新启动了虚拟机。我仍然需要 Web 应用程序的 ServicePointManager.SecurityProtocol |=SecurityProtocolType.Tls12; 行,但除此之外,问题实际上已经解决。

关于c# - 无法发送 HTTP 请求,因为请求已中止。 "Could not create SSL/TLS secure channel",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57553300/

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