![](/img/trans.png)
[英].NET The request was aborted: Could not create SSL/TLS secure channel
[英].net httpclient on Azure w/ TLS/SNI: The request was aborted: Could not create SSL/TLS secure channel
类似的问题之前已被问过多次 - 但在将此问题标记为重复之前,请继续阅读。 这些问题中的大多数都是非常古老的。 我已经解决了很多问题和答案,但没有找到合适的解决方案。
我们在 .net 4.5 中有一个 Azure 云服务项目。 它可以毫无问题地连接到我们客户的数十个 API(不一定是云托管的),但单个 API 失败并显示以下错误消息:
请求被中止:无法创建 SSL/TLS 安全通道
我在这里缺少什么?
这是我用来连接到 API 的代码(稍微精简)(它按 API 运行,因此基本 URL 不会更改):
ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls12 |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls |
SecurityProtocolType.Ssl3;
ApiClient = HttpClientFactory.Create();
ApiClient.DefaultRequestHeaders.Authorization = null;
ApiClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Basic {passwordToken}");
ApiClient.DefaultRequestHeaders.Accept.Clear();
ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
SemaphoreSlim throttler = new SemaphoreSlim(initialCount: 50);
var sp = ServicePointManager.FindServicePoint(new Uri(baseUrl));
sp.SetTcpKeepAlive(true, 30000, 30000);
foreach (var request in urls)
{
Result = new HttpResponseMessage();
Result = await ApiClient.GetAsync(url);
...
}
这就是难以调试的原因:
从有关 .net Framework 中 SNI 的其他问题/答案中考虑的建议:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
行实际上应该启用它。 我这样做的方式有什么问题吗?curl --user 'user:pwd' https://myurl
在 curl(WSL 和 Azure 远程 bash)中调用 API 也可以完美运行并返回预期数据。openssl s_client -connect hostname:443 -tls1_2
没有发现任何问题。 链显示正确,并确认了 TLSv1.2 会话。 使用 openssl 和openssl s_client -connect host:443 -tls1_2 -servername host -tlsextdebug -msg
测试服务器的 SNI 功能,通过返回TLS server extension "server name" (id=0), len=0
显示 SNI 支持,我得到相同的证书如果我提供一个完全不同的幻想主机名。堆栈跟踪:
System.Net.Http.HttpRequestException:发送请求时出错。 ---> System.Net.WebException:请求被中止:无法创建 SSL/TLS 安全通道。 在 System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) --- 内部异常堆栈跟踪结束 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task)任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 WCFServiceWebRole1.Controllers... 在 [调用 GetAsync() 的行]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.