HttpWebRequest访问https请求被中止: 未能创建 SSL/TLS 安全通道

编写代码,使用HttpWebRequest访问网页,错误提示:

未能创建 SSL/TLS 安全通道

页面本来是Http网址,突然转换为https网址,所以遇到这个问题也算正常。将解决方法记录下来给需要的人。

csharp 复制代码
//只需要在 HttpWebRequest 发送请求前加入下面的代码
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

//.Net 4.0版本使用下面代码
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;

参考网址:https://www.cnblogs.com/fogwang/p/14462711.html

相关推荐
针叶8 小时前
Google Play加固保护导致的崩溃
android·安全·google
黎阳之光9 小时前
视频孪生智护供水生命线:黎阳之光赋能医疗与园区水务高质量升级
运维·物联网·算法·安全·数字孪生
德迅云安全-小潘11 小时前
构筑网站DDoS安全防护的演进之路
安全·ddos
Sagittarius_A*11 小时前
H3CSE 高性能园区网:园区网安全体系详解
网络·计算机网络·安全·h3cse
難釋懷13 小时前
Nginx-CA 签名
服务器·nginx·ssl
你今天努力了吗?*—*15 小时前
实践1: Linux 系统运维环境搭建与自动化实践
安全·系统安全
海域云-罗鹏15 小时前
企业服务器遭到网络攻击该如何解决?应急处理与长效防护方案
运维·服务器·安全
TMT星球15 小时前
齐向东:AI时代,三类安全需求集中爆发
人工智能·安全
一拳一个娘娘腔16 小时前
【SRC漏洞挖掘系列】第17期:漏洞组合拳(Chain Exploit)—— 把“蚊子”养成“恐龙”
网络·安全·web安全
汤愈韬16 小时前
四种 NAT 类型详解|透彻理解 NAT 穿越原理(全锥 / 受限锥 / 端口受限锥 / 对称 NAT)
网络·网络协议·安全·网络安全·security