使用c#Webclient通过https请求html

RRa*_*dix 14 c# ssl https webclient

我正在通过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));
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*tin 23

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

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

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

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
Run Code Online (Sandbox Code Playgroud)

这是一个完全有效的例子:

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;
    }
Run Code Online (Sandbox Code Playgroud)

  • 有4个SecurityProtocolType选项:Ssl3,Tls,Tls11,Tls12.我不得不尝试每个人发现Tls12为我工作. (3认同)

小智 12

在.net Framework 4.0中添加

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //TLS 1.2
Run Code Online (Sandbox Code Playgroud)

  • 这对我也很有效(`ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;`)。可能取决于服务器,因此您应该尝试SecurityProtocolType枚举类型https://docs.microsoft.com/zh-cn/dotnet/api/system.net.securityprotocoltype?view=netframework-4.7.2中的所有值。还要添加一个ServerCertificateValidationCallback,它在其他响应中返回true。 (2认同)

Pie*_*rre 5

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

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

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

  • @Pierre 我听从了你的建议,有人下载了我的车。 (8认同)