C# 请求被中止: 未能创建 SSL/TLS 安全通道。 设置SecurityProtocol无效
willgon 人气:0今天为了获取一张图片,用了一段代码:
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback((a, b, c, d) => true); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; ServicePointManager.Expect100Continue = false; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(imgUrl); req.AllowAutoRedirect = true; req.KeepAlive = true; req.Method = "GET"; WebResponse resp = req.GetResponse(); Stream stream = resp.GetResponseStream();
里面添加了ServicePointManager.SecurityProtocol,但是一直还是报错:"请求被中止: 未能创建 SSL/TLS 安全通道。"
然后网上参考了一下:https://blog.csdn.net/yunwu009/article/details/80768860
下载了证书,加到代码中:
string filePath =".\\cer\\specifier.cer"; X509Certificate2 cert = new X509Certificate2(filePath); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(imgUrl); //将证书添加客户端证书集合 req.ClientCertificates.Add(cert);
但是仍然无效。
感觉非常奇怪,然后通过DevTools看了一下图片网址的协议版本:
发现版本已经到了TLS 1.3了,因为我本地的版本是.net framework 4.7 ,还没有支持Tls1.3版本,于是修改了ServicePointManager.SecurityProtocol:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | (SecurityProtocolType)12288;
结果换了新的报错:不支持请求的安全协议。
网上说只有最新的.net 5里面才支持了Tls1.3版本,那.net framework系列就没有解决办法了吗?
该怎么解决?求助各位大神!!!
加载全部内容