C#解决WebClient不能下载https网页内容

在下载之前,执行以下代码即可:

            if (stUrl.Substring(0, 5) == "https")
            {
                // 解决WebClient不能通过https下载内容问题
                ServicePointManager.ServerCertificateValidationCallback +=
                    delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                             System.Security.Cryptography.X509Certificates.X509Chain chain,
                             System.Net.Security.SslPolicyErrors sslPolicyErrors)
                    {
                        return true; // **** Always accept
                    };
            }

上述代码主要是为了跳过https的ssl验证。

using (WebResponse wr = req.GetResponse())

我是在using之前加的上面的代码,各位依据自己的需求进行添加。

转自 :https://blog.csdn.net/qq_37829681/article/details/81200107

posted @ 2020-09-28 15:26  Sir奔  阅读(864)  评论(0编辑  收藏  举报