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 分钟前
信息安全意识之安全组织架构图
数据库·安全·php
Enjoy_zhuo12 分钟前
文件上传漏洞(条件竞争)
安全·web安全
安全检测中13 分钟前
红队OPSEC(安全运营)个人总结
安全
码叔义42 分钟前
X509TrustManager信任SSL证书
python·网络协议·ssl
奔跑吧邓邓子2 小时前
【商城实战(23)】筑牢安全防线,防范常见漏洞
安全·springboot·uniapp·csrf·element plus·sql注入·商城实战
lsrsyx2 小时前
中信银行太原分行营业部开展“金融知识普及共筑消费安全”宣传活动
安全·金融
Hacker_LaoYi2 小时前
Windows server网络安全
windows·安全·web安全
WoTrusSSL2 小时前
小程序SSL证书过期怎么办?
网络协议·小程序·ssl
渗透测试老鸟-九青2 小时前
我与红队:一场网络安全实战的较量与成长
运维·服务器·网络·经验分享·安全·web安全·代码审计
没资格抱怨2 小时前
Http和Https的区别
网络协议·http·https