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

相关推荐
中科数测1 小时前
开源软件惊现高危漏洞,中科固源解决方案利用GDB 调试成关键 “排雷兵”,实战运用指南
安全
AAA修煤气灶刘哥3 小时前
《从 0 到 1 上手:RBAC+SpringSecurity 权限管理教程》
java·后端·安全
深盾安全7 小时前
使用Frida实现Hook,修改接口调用
安全
2301_8016730114 小时前
8.19笔记
网络·安全
zskj_zhyl21 小时前
家庭健康能量站:微高压氧舱结合艾灸机器人,智享双重养生SPA
人工智能·科技·安全·机器人
肥仔哥哥193021 小时前
安全设计-防止非法移机
安全·非法移机·自助机的安全设计·设备的安全设计
网络安全创新研究院1 天前
飞网揭秘:个人网络与团队网络功能对比
安全
唐叔在学习1 天前
万字长文深度解析HTTPS协议
后端·https
Warren981 天前
软件测试-Selenium学习笔记
java·javascript·笔记·学习·selenium·测试工具·安全
字节跳动安全中心1 天前
智能体防御 | 一文了解3种系统提示词加固方法
安全·llm