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

相关推荐
Clownseven4 小时前
云端备份与恢复策略:企业如何选择最安全的备份解决方案
安全
拾光拾趣录6 小时前
无状态协议下的用户状态管理:Web应用如何保持用户登录态
前端·http·https
薄荷椰果抹茶7 小时前
【网络安全基础】第六章---Web安全需求
安全·web安全
游戏开发爱好者814 小时前
iOS重构期调试实战:架构升级中的性能与数据保障策略
websocket·网络协议·tcp/ip·http·网络安全·https·udp
HumanRisk14 小时前
降低网络安全中的人为风险:以人为本的路径
网络·安全·web安全
运维开发王义杰15 小时前
金融安全生命线:用AWS EventBridge和CloudTrail构建主动式入侵检测系统
安全·金融·aws
安全系统学习17 小时前
系统安全之大模型案例分析
前端·安全·web安全·网络安全·xss
加密狗复制模拟18 小时前
坚石ET ARM加密狗复制模拟介绍
安全·软件工程·个人开发
galaxylove19 小时前
Gartner发布塑造安全运营未来的关键 AI 自动化趋势
人工智能·安全·自动化
scuter_yu19 小时前
主流零信任安全产品深度介绍
运维·网络·安全