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

相关推荐
孫治AllenSun14 分钟前
【系统安全】信息脱敏存储:场景、需求与实现方案详解
安全·系统安全·php
硅谷秋水39 分钟前
迈向具身智能体的驾驭
人工智能·深度学习·安全·机器学习·语言模型·机器人
zhaodezhu16881 小时前
四维技术全域赋能 一网推重构企业数字营销增长新范式
安全·架构·泰兴geo优化·可靠公司·苏州geo技术·四维技术·全域赋能
愚公搬代码2 小时前
【愚公系列】《Web应用安全》012-Behinder工具的使用
前端·安全
Linux运维技术栈3 小时前
云平台服务器遭遇黑客攻击?用 Nginx 批量封锁敏感接口,自动返回 444 关闭连接
运维·服务器·nginx·安全·防御
guwentian3 小时前
从0到1手写 AI Agent Harness:为什么护城河不在模型,而在工程外壳
人工智能·python·安全·deepseek·harness
何以解忧,唯有..3 小时前
HTTP 与 HTTPS:从明文传输到安全加密的演进
安全·http·https
ybdesire4 小时前
注入型漏洞以及agent-audit的检测原理
网络·安全·风险·智能体·ai安全
三垣网安13 小时前
一次学校系统水平越权漏洞的实战测试记录
安全
蓝速科技16 小时前
蓝速科技信创落地:平衡安全合规与项目成本的实战方案
android·运维·人工智能·科技·安全·电脑·鸿蒙