Well, I solved it myself. Answer from this StackOverflow question, which has more votes, helped me to solve it.
My sample as follows:
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;
}
I still wonder how does it work properly? Because new RemoteCertificateValidationCallback(ValidRemoteCertificate)
is not taking dropboxUrl
from anywhere. So how X509Certificate certificate
param in ValidRemoteCertificate
method got the correct certificate from Dropbox.com?