HTTPS使用WebDownloadClient的URL会引发异常

Pri*_*tam 5 .net c# https

我有一个https URL,抛出异常:

异常详细信息:System.Exception:基础连接已关闭:发送时发生意外错误.---> System.Net.WebException:底层连接已关闭:发送时发生意外错误.---> System.IO.IOException:从传输流中收到意外的EOF或0字节.

我尝试使用HTTP版本的URL,这是有效的.我还检查了是否可以从计算机访问https URL,并且可以访问它们而没有任何错误,但是在下面的代码中失败了.此问题也是机器特定的.

抛出错误的代码行:

 using (WebDownloadClient wc = new WebDownloadClient())
 {
     wc.Headers.Add("Content-Encoding", "gzip");
     wc.Headers.Add("Accept-Encoding", "gzip, compress");
     wc.DownloadFile(url, fileName);
 }
Run Code Online (Sandbox Code Playgroud)

小智 2

    ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 ;

    using (WebDownloadClient wc = new WebDownloadClient())
    {
        wc.Headers.Add("Content-Encoding", "gzip");
        wc.Headers.Add("Accept-Encoding", "gzip, compress");
        wc.DownloadFile(url, fileName);
    }

private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
    if (error == System.Net.Security.SslPolicyErrors.None)
    {
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

取自使用c# Webclient 通过 https 请求 html