无法从Dropbox下载文件,因为无法通过C#中的WebClient与SSL / TLS通道连接

问题描述 投票:1回答:1

尝试使用WebClient.DownloadFile()将可执行文件从Dropbox的私人文件夹下载到Windows服务中的PC。但它正在抛出错误底层连接已关闭:无法为SSL / TLS安全通道建立信任关系。

我们所遇到的是,错误仅发生在Windows XP(SP2)中,而不发生在Windows 7,8和8.1中。 (尚未在XP SP3和Vista中测试过。)

试过:

  • WebClient.UseDefaultCredentialstrue
  • WebClient.Credentials = CredentialCache.DefaultNetworkCredentialsCredentialCache.DefaultCredentials
  • http://在URL而不是https://
ssl windows-services webclient dropbox webclient-download
1个回答
1
投票

好吧,我自己解决了。得到更多选票的this StackOverflow question的答案帮助我解决了这个问题。

我的样本如下:

public static void DownloadFileFromDropbox(string dropboxUrl, string file_name)
    WebClient webclient = null;
    try
    {
        webclient = new WebClient();

        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidRemoteCertificate);

        if (File.Exists(UpdatesDirStr + file_name))
        {
            File.Delete(UpdatesDirStr + file_name);
        }
        webclient.DownloadFile(dropboxUrl, UpdatesDirStr + file_name);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        if (webclient != null)
        {
            webclient.Dispose();
            webclient = null;
        }
    }
}

private static bool ValidRemoteCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    if (certificate.Subject.Contains("dropboxusercontent.com"))
    {
        return true;
    }
    else if (certificate.Subject.Contains("dropbox.com"))
    {
        return true;
    }
    return false;
}

我仍然想知道它是如何正常工作的?因为new RemoteCertificateValidationCallback(ValidRemoteCertificate)没有从任何地方采取dropboxUrl。那么X509Certificate certificate方法中的ValidRemoteCertificate param如何从Dropbox.com获得正确的证书?

© www.soinside.com 2019 - 2024. All rights reserved.