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

相关推荐
zhengfei61121 小时前
AI渗透工具——基于大型模型的自主渗透测试智能体鸾鸟(LuaN1ao)
安全·ai·开源
还鮟1 天前
靶机远程控制实验命令与入门实践(Linux)
linux·网络·安全
未来之窗软件服务1 天前
幽冥大陆(六十六) PHP8.x SSL 文字解密—东方仙盟古法结界
ssl·加解密·仙盟创梦ide·东方仙盟
科技块儿1 天前
【账号安全预警】如何基于IP查询进行登录异常识别、账号防盗?
网络协议·tcp/ip·安全
emma羊羊1 天前
【AWVS】漏扫工具
网络·安全·web安全·awvs
2501_915918411 天前
iOS 开发中证书创建与管理中的常见问题
android·ios·小程序·https·uni-app·iphone·webview
网安INF1 天前
入侵检测系统(IDS)解析
网络·网络协议·安全·网络安全·ids
00后程序员张1 天前
IOScer 开发环境证书包括哪些,证书、描述文件与 App ID 的协同管理实践
android·ios·小程序·https·uni-app·iphone·webview
catoop1 天前
网站安全加固:优化 Nginx 安全头配置
nginx·安全
萤丰信息1 天前
数智重构生态:智慧园区引领城市高质量发展新范式
java·大数据·人工智能·安全·智慧城市