【问题标题】:Requesting html over https with c# Webclient使用 c# Webclient 通过 https 请求 html
【发布时间】:2013-12-02 14:09:43
【问题描述】:

我正在尝试通过 c# WebClient 类从我无法控制的站点中获取各种 html 资源。 当我尝试访问诸如 "https://archive.org/details/OTRR_In_The_Name_Of_The_Law_Singles"

我收到错误: System.Net.WebException:请求被中止:无法创建 SSL/TLS 安全通道。

我找到了建议我使用以下代码忽略证书要求并使 webclient 充当浏览器的解决方案,但我仍然收到相同的错误

 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(
            delegate
            {
                return true;
            });
            using(WebClient webClient = new WebClient()) {
                webClient.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)";
                webClient.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                webClient.Headers["Accept-Language"] = "en-us,en;q=0.5";
                webClient.Headers["Accept-Encoding"] = "gzip,deflate";
                webClient.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
                StreamReader sr = new StreamReader(webClient.OpenRead(inputString));
}

【问题讨论】:

  • 你在代理后面吗?
  • 我可能是,我正在使用托管服务器。考虑到我在代理背后,有什么解决方案?
  • 刚刚联系了托管我服务器的朋友,他告诉我我没有使用代理。

标签: c# ssl https webclient


【解决方案1】:

我试过这个例子并收到错误 "请求中止:无法创建 SSL/TLS 安全通道"

要解决此问题,可以在我的案例 Tls12 中更改 SecurityProtocol,它运行良好。

【讨论】:

    【解决方案2】:

    在 .net Framework 4.0 中添加

    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //TLS 1.2
    

    【讨论】:

    • 节省我的时间!谢谢!
    • 这对我也有用(ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;)。可能它取决于服务器,因此您应该尝试 SecurityProtocolType 枚举类型docs.microsoft.com/en-us/dotnet/api/… 中的所有值。但还要添加一个 ServerCertificateValidationCallback ,它与其他响应一样返回 true。
    • 对我来说,这是指定 Ssl 或 Tls 以外的任何内容的唯一方法(VS 2017)
    【解决方案3】:

    只需在var stream = webClient.OpenRead(address);之前添加这一行

    System.Net.ServicePointManager.ServerCertificateValidationCallback += (send, certificate, chain, sslPolicyErrors) => { return true; };
    

    这应该可以解决 SSL/TLS 错误

    【讨论】:

    • 您给出的建议忽略了证书错误,因此可能并不明智。
    • @DavidMartin 同意。但这一切都取决于它使用的上下文。有时它是否安全并不重要。但我猜每个人都有自己的想法。
    • @Pierre 我听从了你的建议,有人下载了我的车。
    【解决方案4】:

    阅读:http://support.microsoft.com/kb/915599

    您正在访问的服务器不支持 TLS,因此您需要强制它使用 SSL3。

    将以下行添加到您的通话中:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
    

    这是一个完整的示例:

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    
    class Program
    {
        static void Main(string[] args)
        {
            Uri address = new Uri("https://archive.org/details/OTRR_In_The_Name_Of_The_Law_Singles");
    
            ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 ;
    
            using (WebClient webClient = new WebClient())
            {
                var stream = webClient.OpenRead(address);
                using (StreamReader sr =new StreamReader(stream))
                {
                    var page = sr.ReadToEnd();
                }
            }
        }
    
        /// <summary>
        /// Certificate validation callback.
        /// </summary>
        private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
        {
            // If the certificate is a valid, signed certificate, return true.
            if (error == System.Net.Security.SslPolicyErrors.None)
            {
                return true;
            }
    
            Console.WriteLine("X509Certificate [{0}] Policy Error: '{1}'",
                cert.Subject,
                error.ToString());
    
            return false;
        }
    

    【讨论】:

    • KB 915599 面向 .NET 1.1 SP 1,OP 至少面向 .NET 3.5
    • SecurityProtocolType 有 4 个选项:Ssl3、Tls、Tls11、Tls12。我必须尝试每一个才能发现 Tls12 对我有用。
    猜你喜欢
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2013-02-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    相关资源
    最近更新 更多