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

相关推荐
jieyu11192 小时前
网络、主机安全扫描工具
linux·安全·系统安全
FIN66683 小时前
昂瑞微冲刺科创板:创新驱动,引领射频芯片国产化新征程
前端·安全·前端框架·信息与通信·芯片
用户47949283569158 小时前
面试官:讲讲2FA 双因素认证原理
前端·后端·安全
Mintimate10 小时前
Vue项目接口防刷加固:接入腾讯云天御验证码实现人机验证、恶意请求拦截
前端·vue.js·安全
网安INF10 小时前
网络攻防技术:网络安全攻击概述
安全·web安全·网络安全·网络攻防
深盾安全12 小时前
C++实战:快速提取Android APK数字签名
安全
white-persist12 小时前
SQL 注入详解:从原理到实战
前端·网络·数据库·sql·安全·web安全·原型模式
AORO202512 小时前
防爆手机与普通手机的区别!
网络·5g·安全·智能手机·电脑·信息与通信
携欢13 小时前
Portswigger靶场之Exploiting a mass assignment vulnerability通关秘籍
前端·安全
嗨丶王哪跑13 小时前
网络安全审计技术原理与应用
运维·网络·安全·web安全