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/