繁体   English   中英

即使在指定ServicePointManager.SecurityProtocol之后,HttpWebRequest中的“请求已中止:无法创建SSL / TLS安全通道”

[英]“The request was aborted: Could not create SSL/TLS secure channel” in HttpWebRequest, even after specifying ServicePointManager.SecurityProtocol

为了满足PCI合规性,我们必须从服务器上完全禁用TLS 1.0。 自从这样做以来,我不得不指定在发出Web请求时要使用哪种协议才能使工作正常。 但是,一个特殊的问题仍然存在。 即使指定了协议,我仍然会收到标题中指出的错误。

var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    streamWriter.Write(body);
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
    return result;
}

错误本身是从该行生成的-

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

如您所见,我指定使用TLS 1.1或1.2(我本来只在1.2上使用它,但认为也允许1.1会有所帮助,但实际上并没有)。

另外(不确定这是否相关),此特定代码段始终在调用我们也要运行和维护的API,因此从本质上讲,服务器正在调用自身以获取所需的信息。

而且我很快就没有办法进一步调试或解决此问题,因此,我们将不胜感激。

我终于弄明白了,解决方案比我想承认的要简单得多。 我只是移动线

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;

到我在问题中发布的代码片段的最顶端,此后一切都一直有效。 我猜它需要在创建HttpWebRequest之前就位吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM