代码之家  ›  专栏  ›  技术社区  ›  rudigrobler

WebClient+HTTPS问题

  •  62
  • rudigrobler  · 技术社区  · 17 年前

    我目前正在与第三方创建的系统集成。此系统要求我使用XML/HTTPS发送请求。第三方向我发送了证书,我安装了它

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

    此代码返回以下内容 WebException :

    基础连接已关闭:无法为SSL/TLS安全通道建立信任关系。

    更新 ServerCertificateValidationCallback

    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;
    }
    

    here here

    3 回复  |  直到 11 年前
        1
  •  71
  •   Koen Zomers    15 年前

    允许所有证书的最短代码表示法实际上是:

    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    

    并且很好地解决了这个错误。不用说,您应该提供一个实际检查证书并根据证书信息决定通信是否安全的实现。出于测试目的,使用上面的代码行。

        2
  •  8
  •   atconway    15 年前

    对于原始答案的VB.NET版本,如下所示(当需要使用“AddressOf”运算符连接事件时,转换器工作不好)。使用WebClient()或HttpWebRequest()对象之前的第一个代码:

    ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf bypassAllCertificateStuff)
    

    ..和连线方法代码:

    Private Shared Function bypassAllCertificateStuff(ByVal sender As Object, ByVal cert As X509Certificate, ByVal chain As X509Chain, ByVal [error] As System.Net.Security.SslPolicyErrors) As Boolean
        Return True
    End Function
    
        3
  •  -1
  •   Bo Persson Touseef    14 年前

    试试这个,它奏效了:

    class Ejemplo
    {
        static void Main(string[] args)
        {
            string _response = null;
            string _auth = "Basic";
            Uri _uri = new Uri(@"http://api.olr.com/Service.svc");
    
            string addres = @"http://api.olr.com/Service.svc";
            string proxy = @"http://xx.xx.xx.xx:xxxx";
            string user = @"platinum";
            string pass = @"01CFE4BF-11BA";
    
    
            NetworkCredential net = new NetworkCredential(user, pass);
            CredentialCache _cc = new CredentialCache();
    
            WebCustom page = new WebCustom(addres, proxy);
            page.connectProxy();
    
            _cc.Add(_uri, _auth, net);
    
            page.myWebClient.Credentials = _cc;
    
            Console.WriteLine(page.copyWeb());
        }
    
    }
    
    public class WebCustom
    {
            private string proxy;
            private string url;
            public WebClient myWebClient;
            public WebProxy proxyObj;
            public string webPageData;
    
    
            public WebCustom(string _url, string _proxy)
            {
                url = _url;
                proxy = _proxy;
                myWebClient = new WebClient();
            }
    
            public void connectProxy()
            {
                proxyObj = new WebProxy(proxy, true);
                proxyObj.Credentials = CredentialCache.DefaultCredentials;
                myWebClient.Proxy = proxyObj;
            }
    
            public string copyWeb()
            { return webPageData = myWebClient.DownloadString(url); }
    }