c# - System.Net.HttpWebRequest.GetResponse() 错误 "ResponceSystem.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel"

标签 c# winforms ssl

Dotnet 应用程序是使用 dotnet 4.5 构建的。在一些系统上,我们在连接到最近对 SSL 进行了一些更改的 Web 服务器时遇到此错误。

Web 服务器仅 TLS 1.2 https://drive.google.com/open?id=1Z0S-MWugDZdQrIy3BnquooB0ZX7veIVT

客户端代码。

WebRequest webRequest = WebRequest.Create(strUrl);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "Get";
if (strUrl.StartsWith("HTTPS", StringComparison.OrdinalIgnoreCase)) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
using (WebResponse webResponse = webRequest.GetResponse())
{
    if (webResponse == null) return blnResult;
    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
    {
       strSuccess = sr.ReadToEnd().Trim();
       blnResult = true;
    }
}

只要我用框架 4.6.2 编译它,它就可以工作。

为什么它在少数具有相同 4.5 编译 EXE 的系统上工作,而在少数系统上需要使用 4.6.2 编译?

WebClient 类适用于 4.5 版本的 dotnet 框架。只有 WebRequest 不工作。

最佳答案

初始化WebRequest对象前需要先设置安全协议(protocol)。

if (strUrl.StartsWith("HTTPS", StringComparison.OrdinalIgnoreCase)) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

WebRequest webRequest = WebRequest.Create(strUrl);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "Get";

using (WebResponse webResponse = webRequest.GetResponse())
{
    if (webResponse == null) return blnResult;
    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
    {
       strSuccess = sr.ReadToEnd().Trim();
       blnResult = true;
    }
}

关于c# - System.Net.HttpWebRequest.GetResponse() 错误 "ResponceSystem.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56269532/

相关文章:

c# - 为什么程序使用添加到委托(delegate)的最后一个方法?

c# - AsParallel() 顺序执行

c# - 检查暂停布局

c# - CellDoubleClick 与 CellMouseDoubleClick

ruby-on-rails - 使用 https 保护 Rails API

java - 使用安装的 java keystore 证书连接到 ldap

c# - 通过构造函数在 IoC 中强制执行依赖项?

c# - 通过 Fluent API 的 EF Core 外键

c# - 一个接一个改变标签的颜色

IIS 证书。网站关系证明