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

相关推荐
Arwen3033 小时前
IP地址证书的常见问题有哪些?有没有特殊渠道可以申请免费IP证书?
服务器·网络·网络协议·tcp/ip·http·https
蝎蟹居3 小时前
GBT 4706.1-2024逐句解读系列(26) 第7.6条款:正确使用符号标识
人工智能·单片机·嵌入式硬件·物联网·安全
GISer_Jing3 小时前
AI Agent 智能体的“深度思考”与“安全防线”
人工智能·学习·安全·aigc
DBA小马哥3 小时前
金仓数据库引领国产化替代新范式:构建高效、安全的文档型数据库迁移解决方案
数据库·安全·mongodb·dba·迁移学习
卓豪终端管理4 小时前
BYOD时代的安全革命:工作空间隔离如何重塑企业移动管理
安全
duyinbi75177 小时前
改进YOLO13模型:C3k2与PPA优化在油田工人安全装备检测与行为识别中的应用
人工智能·安全·目标跟踪
凯丨8 小时前
使用 frp 实现内网穿透:让本地服务器安全暴露到公网
运维·服务器·安全
下海fallsea8 小时前
德邦跟了京东,极兔搂住顺丰
网络·人工智能·安全
iSee8579 小时前
struts2 XML外部实体注入漏洞复现(CVE-2025-68493)
xml·安全·struts2
OpenCSG9 小时前
OpenCSG 生态核心:CSGHub 构建企业 AI 全生命周期管理体系,安全与协同双向赋能
人工智能·安全