【问题标题】:Error with download a file from https using webclient使用 webclient 从 https 下载文件时出错
【发布时间】:2014-01-24 04:52:36
【问题描述】:

我创建了一段使用 WebClient 从给定 URL 下载文件的代码。

问题是当代码尝试从HTTPS 站点下载文件时出现以下错误。

The remote certificate is invalid according to the validation procedure

这个问题只发生在服务器上,不在本地机器上,所以我也不知道如何调试它。

我在网上阅读了几个答案,但没有找到任何可以帮助我的答案。

这段代码:

 using (WebClient Client = new WebClient())
        {
            string fileName = System.Configuration.ConfigurationManager.AppSettings["siteUploadedDirectory"] 
            + "/temp/" + Context.Timestamp.Ticks.ToString() 
            + "_" + FileURL.Substring(FileURL.LastIndexOf('/')+1);
            fileName = Server.MapPath(fileName);
            Client.DownloadFile(FileURL, fileName);
            return fileName + "|" + FileURL.Substring(FileURL.LastIndexOf('/')+1);
        }

我正在尝试的网址:

http://Otakim.co.il/printreferrer.aspx?ReferrerBaseURL=cloudents.com
&ReferrerUserName=ram
&ReferrerUserToken=1
&FileURL=https://www.cloudents.com/d/lzodJqaBYHu/pD0nrbAtHSq

文件网址: FileURL=https://www.cloudents.com/d/lzodJqaBYHu/pD0nrbAtHSq

任何帮助将不胜感激

【问题讨论】:

    标签: c# asp.net .net https webclient-download


    【解决方案1】:

    您可以通过以下代码片段绕过证书验证过程

        ServicePointManager.ServerCertificateValidationCallback
     += (sender, certificate, chain, sslPolicyErrors) => true;
    

    【讨论】:

    • 你在哪里使用这个 lambda 表达式
    • 就在“字符串文件名=”行之前
    • 如果您不喜欢代码中的 Lambda 表达式,这是 VB 中的完整方法定义Public Shared Function ServerCertificateValidationCallback(sender As Object, cert As System.Security.Cryptography.X509Certificates.X509Certificate, chain As System.Security.Cryptography.X509Certificates.X509Chain, optional_item As Object) As Boolean Return True End Function
    猜你喜欢
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    相关资源
    最近更新 更多