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

相关推荐
梦昼初DawnDream5 小时前
linux安全基线
linux·运维·安全
档案宝档案管理6 小时前
档案宝:企业合同档案管理的“安全保险箱”与“效率加速器”
大数据·数据库·人工智能·安全·档案·档案管理
星释6 小时前
二级等保实战:MySQL安全加固
android·mysql·安全
闲人编程6 小时前
Python与区块链:如何用Web3.py与以太坊交互
python·安全·区块链·web3.py·以太坊·codecapsule
碎碎思7 小时前
FPGA新闻速览-从漏洞到突破:FPGA技术在安全、架构与量子领域
安全·fpga开发
民乐团扒谱机8 小时前
实验室安全教育与管理平台学习记录(八)特种设备安全
学习·安全
ozawacai10 小时前
详细了解TLS、HTTPS、SSL原理
计算机网络·https·ssl
2501_9159184110 小时前
HTTP抓包工具推荐,Fiddler使用教程、代理设置与调试技巧详解(含HTTPS配置与实战案例)
http·ios·小程序·https·fiddler·uni-app·webview
国科安芯11 小时前
抗辐照MCU芯片在无人叉车领域的性能评估与选型建议
网络·人工智能·单片机·嵌入式硬件·安全
xixixi7777712 小时前
水坑攻击的攻击原理和特点+案例和防御方法
网络·安全·web安全