解决HttpWebRequest访问https请求被中止: 未能创建 SSL/TLS 安全通道
2023-09-11 14:19:35 时间
处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。)只需加上以下两行代码就行了。//处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。)ServicePointManager.ServerCertificateValidation…
处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。)
只需加上以下两行代码就行了。
//处理HttpWebRequest访问https有安全证书的问题( 请求被中止: 未能创建 SSL/TLS 安全通道。) ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
以上代码是.Net 4.5以上版本可以直接使用。
.Net 4.0版本使用下面代码
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true; ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
示例:
/*发送微信订阅通知消息*/ private string sendWxNotify(string name,string tel,int goodsId, HttpContext context) { ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true; ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072; HttpWebRequest requet = (HttpWebRequest)WebRequest.Create("https://xx/postmessage?key=ad&name="+name+"&tel="+tel+"&id="+goodsId.ToString()); requet.Method = "GET"; using (HttpWebResponse response = (HttpWebResponse)requet.GetResponse()) { StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.Default); return sr.ReadToEnd(); } return ""; }
相关文章
- PHP 配置默认SSL CA证书
- php cURL error 60: SSL certificate problem: unable to get local issuer certificate 解决办法
- There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:661) - skipping
- HTTPS-加密SSL证书
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)
- keras 下载预训练模型报错SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)
- JavaMail给QQ邮箱发邮件报错,没有SSL加密
- SSL安全解决方案(转)
- 抓包分析SSL/TLS连接建立过程【总结】
- Invoke-WebRequest : 请求被中止: 未能创建 SSL/TLS 安全通道。
- SSL握手步骤【收藏】
- 搭建Jasig CAS中央认证服务实现单点登录——搭建Tomcat并实现SSL安全连接
- Android 4.4 网络请求报错:javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL hand
- 100集华为HCIE安全培训视频教材整理 | SSL VdPdNd概述
- wget命令出现Unable to establish SSL connection.错误
- SSL连接建立过程分析(1)
- SSL证书类型DV SSL、OV SSL和EV SSL区别选择方法