zoukankan      html  css  js  c++  java
  • 【SSL】WebClient 请求 https 页面出错:未能创建 SSL/TLS 安全通道

    #问题:

      当向一个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 }
  • 相关阅读:
    div 圆角
    CSS定义鼠标经过时鼠标图型样式
    如何判断浏览器类型然后让它读取指定的CSS
    如何分别指定ie6及ie7浏览器的css
    用CSS控制DIV居中失效的解决方法
    css如何控制文字多行显示,溢出截断后末尾出现省略...
    样式命名规则
    type="file" 谁用过这个属性给定义样式
    有利于SEO的DIV+CSS的命名规矩小结
    左右两个div高度自动一致,自适应高度
  • 原文地址:https://www.cnblogs.com/willingtolove/p/9403796.html
Copyright © 2011-2022 走看看