分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

【SSL】WebClient 请求 https 页面出错:未能创建 SSL/TLS 安全通道

发布时间:2023-09-06 02:07责任编辑:林大明关键词:Webhttp

#问题:

  当向一个https的url上发送请求,报错:未能创建 SSL/TLS 安全通道;

using (WebClient client = new WebClient()){ ???string address="https://xxx.com"; ???client.Headers.Add(HttpRequestHeader.ContentType, "text/xml"); ???System.Text.ASCIIEncoding ?encoding=new System.Text.ASCIIEncoding(); ???var response = client.UploadData(address, "POST", encoding.GetBytes(msg));}

 #原因:

  ssl证书不受信任,验证失败;

#解决方案:

  请求之前,进行如下设置;

ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(bypassAllCertificateStuff);private static bool bypassAllCertificateStuff(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error){ ??return true;}

  简写

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

  整合

 1 public class Test 2 { 3 ????public void TestFun 4 ????{ 5       ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 6        using (WebClient client = new WebClient()) 7       { 8 ???      ?string address="https://xxx.com";
9         ?client.Headers.Add(HttpRequestHeader.ContentType,"text/xml");10 ???      System.Text.ASCIIEncoding ?encoding=new System.Text.ASCIIEncoding();11 ???      var response = client.UploadData(address, "POST", encoding.GetBytes(msg));12       }13 ????} 14 ????private bool CheckValidationResult(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors errors)15 ????{ ??// 总是接受 认证平台 服务器的证书16 ????????return true;17 ????}18 }

【SSL】WebClient 请求 https 页面出错:未能创建 SSL/TLS 安全通道

原文地址:https://www.cnblogs.com/willingtolove/p/9403796.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved