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

相关推荐
AI实战10 分钟前
可信的人类与人工智能协作:基于人类反馈和物理知识的安全自主驾驶强化学习
人工智能·安全
朗迪锋29 分钟前
航空维修培训中的虚拟现实辅助工程技术应用
大数据·人工智能·安全·vr·虚拟现实
docuxu43 分钟前
软考-信息安全-基础知识
网络·安全·web安全
小小工匠1 小时前
加密与安全_ sm-crypto 国密算法sm2、sm3和sm4的Java库
java·算法·安全·sm2·sm3·sm4
互联科技报2 小时前
网站被爬,数据泄露,如何应对不断强化的安全危机?
安全
廾匸07052 小时前
《人工智能安全治理框架》1.0版
人工智能·安全
好奇的菜鸟2 小时前
GORM安全-保护你的应用免受SQL注入攻击
数据库·sql·安全
newxtc2 小时前
【Authing身份云-注册安全分析报告-无验证方式导致安全隐患】
安全