【问题标题】:WebClient + HTTPS IssuesWebClient + HTTPS 问题
【发布时间】:2010-10-06 21:16:43
【问题描述】:

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

我使用以下代码:

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

阅读更多 herehere

【问题讨论】:

  • here.
  • +1 用于使用您使用的代码进行更新。因此,对我来说很好的快速修复。
  • 这在调试 SSL web 服务时非常有用,而不是将提琴手 CA 根注册到我的开发机器中!我只是在添加虚拟回调的部分周围放置了一个#if DEBUG,以不将其放入生产代码中。
  • 这绝对适用于 .NET 4.5,但不适用于 4.6。我想知道为什么。

标签: c# https webclient


【解决方案1】:

允许所有证书的代码的最短符号实际上是:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

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

【讨论】:

  • 确实对我的集成测试很有帮助!
  • vb.net 版本:ServicePointManager.ServerCertificateValidationCallback = Function() True
【解决方案2】:

对于原始答案的 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】:

    试试这个,它有效:

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

    【讨论】:

    • 您能否解释一下您的解决方案如何解决 HTTP_S_ 问题?
    猜你喜欢
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多