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

相关推荐
苏打水com39 分钟前
内容合规红线:大模型生成内容,哪些能做哪些会犯法?
人工智能·安全·大模型
2601_962364971 小时前
重装系统后,Windows Hello 为什么要重新录入人脸?
windows·安全·电脑
网教盟人才服务平台2 小时前
Spring4Shell 漏洞原理、利用手段与全场景防御方案
安全·web安全
仙魁XAN2 小时前
【WorkBuddy·基础入门】第五篇 :模型、权限和工作空间:让 WorkBuddy 做得好,也做得安全
人工智能·安全·workbuddy·workbuddy 基础入门
jimmyleeee2 小时前
大模型安全之十三:威胁建模
人工智能·安全
Purple Coder3 小时前
黄浦区-------------------
安全
TunerT_TQ3 小时前
智能体评测的哲学——当“跑分”不再等于“能力”|第0期 · 序章
安全·架构·agent
企业数字化笔记4 小时前
AI写的网站一直自动跳转怎么办?301、302和HTTPS重定向排查
网络协议·http·https
天涯明月19934 小时前
Agent Sandbox 深度解析——给会动手的 AI 一个安全的房间
人工智能·安全·大模型·agent·sandbox