Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c#Webclient通过https请求html_C#_Ssl_Https_Webclient - Fatal编程技术网

使用c#Webclient通过https请求html

使用c#Webclient通过https请求html,c#,ssl,https,webclient,C#,Ssl,Https,Webclient,我正在尝试通过c#WebClient类从一个我无法控制的站点获取各种html资源。 当我尝试访问URL时,例如 “” 我得到一个错误: System.Net.WebException:请求被中止:无法创建SSL/TLS安全通道 我找到了一些解决方案,建议我使用以下代码来忽略证书要求,并使webclient充当浏览器,但我仍然收到相同的错误 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificat

我正在尝试通过c#WebClient类从一个我无法控制的站点获取各种html资源。 当我尝试访问URL时,例如 “”

我得到一个错误: 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));
}
读一读:

您正在访问的服务器不支持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;
    }
使用系统;
使用System.IO;
Net系统;
使用System.Net.Security;
使用System.Security.Cryptography.X509证书;
班级计划
{
静态void Main(字符串[]参数)
{
Uri地址=新的Uri(“https://archive.org/details/OTRR_In_The_Name_Of_The_Law_Singles");
ServicePointManager.ServerCertificateValidationCallback+=ValidateRemoteCertificate;
ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3;
使用(WebClient WebClient=new WebClient())
{
var stream=webClient.OpenRead(地址);
使用(StreamReader sr=新StreamReader(stream))
{
var page=sr.ReadToEnd();
}
}
}
/// 
///证书验证回调。
/// 
私有静态bool ValidateRemoteCertificate(对象发送方、X509证书证书证书、X509链、SslPolicyErrors错误)
{
//如果证书是有效的签名证书,则返回true。
if(error==System.Net.Security.SslPolicyErrors.None)
{
返回true;
}
WriteLine(“X509Certificate[{0}]策略错误:“{1}”,
证书主题,
错误。ToString());
返回false;
}

只需在
var stream=webClient.OpenRead(地址);


这将解决.net Framework 4.0中的SSL/TLS错误

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

我尝试了这个例子并收到了错误 “请求被中止:无法创建SSL/TLS安全通道”

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


你支持代理吗?我可能支持,我使用的是托管服务器。如果我支持代理,解决方案是什么?刚刚联系了我的朋友,他告诉我我不支持代理。KB 915599目标是.NET 1.1 SP 1,OP目标是至少.NET 3.5有4个SecurityProtocolType选项:Ssl3、Tls、Tls11、Tls12。我不得不尝试每一种方法来发现Tls12对我有效。你给出的建议忽略了证书错误,因此可能是不明智的。@DavidMartin同意。但这完全取决于它所使用的上下文。有时它是否安全并不重要。但我想每个人都有自己的想法。@Pierre我听从了你的建议,有人下载了我的车。Sav我的时间!谢谢!这对我也很有用(
ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12;
)。这可能取决于服务器,因此您应该尝试SecurityProtocolType枚举类型中的所有值。但还应添加一个与其他响应中一样返回true的
ServerCertificateValidationCallback
。对于我来说,这是指定Ssl或Tls以外的任何内容的唯一方法(VS 2017)
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //TLS 1.2