无法发送 HTTP 请求,因为请求已中止。 "Could not create SSL/TLS secure channel"
Cannot send a HTTP request because the request gets aborted. "Could not create SSL/TLS secure channel"
我目前正在开发一个 C# Web 应用程序,它使用 Invoice Ninja 的 API 检查付款/发票信息。我已经设法使用 HttpClient 在我的本地机器上运行它,但是每当它被部署到部署服务器(Windows Azure VM)时,我都会收到以下错误:
The request was aborted: Could not create SSL/TLS secure channel.
启用和未启用 SSL 的站点的错误是相同的(开发站点没有,而实时站点有)。
我试过使用以下解决方案:
在创建 HttpClient 之前添加 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
。
使用
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 调用,但不幸的是,同样的事情仍然发生。我仍然收到 "The request was aborted: Could not create SSL/TLS secure channel." 错误。
这可能是某种服务器配置问题吗?
编辑 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# Web 应用程序,它使用 Invoice Ninja 的 API 检查付款/发票信息。我已经设法使用 HttpClient 在我的本地机器上运行它,但是每当它被部署到部署服务器(Windows Azure VM)时,我都会收到以下错误:
The request was aborted: Could not create SSL/TLS secure channel.
启用和未启用 SSL 的站点的错误是相同的(开发站点没有,而实时站点有)。
我试过使用以下解决方案:
在创建 HttpClient 之前添加 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
。
使用
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 调用,但不幸的是,同样的事情仍然发生。我仍然收到 "The request was aborted: Could not create SSL/TLS secure channel." 错误。
这可能是某种服务器配置问题吗?
编辑 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;
行,但除此之外,问题实际上已解决。