2

I have the need to download a file that is hidden behind an HTTPS connection.

I am new to the downloading of files that are sitting behind a secure website, I have tried using credentials to fix this problem but to no avail.

From what I have read, you need to create and use a certificate to complete this, but I have found no examples. Any help is appreciated.

This is what I have for now:

        WebClient dove = new WebClient();
        CredentialCache mycache = new CredentialCache();
        dove.Credentials = new NetworkCredential("user", "pass"); ;
        dove.DownloadProgressChanged += new DownloadProgressChangedEventHandler(dove_DownloadProgressChanged);
        dove.DownloadFileCompleted += new AsyncCompletedEventHandler(dove_DownloadFileCompleted);
        dove.DownloadFileAsync(new Uri("http://secure.website.com/File/Default.aspx"), "file.xls");

Could someone provide me with an example on how to complete this, thank you.

Vasa Serafin
  • 306
  • 5
  • 18

2 Answers2

2

Try:

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, policy) => {
    //do what you want
    return true;
};
Alexey Raga
  • 7,457
  • 1
  • 31
  • 40
2

.net support https certificate transparently and u do not need to provide it explicitly. Your code looks good and it should work as long as your website does not need forms authentication. From your code it looks authentication might be basic authentication. However if form authentication is required which normally does for websites then you first need to write a code which authenticate and recieve authenticated cookie from the server. How to do it? Better look at this SO question HttpClient and forms authentication in C#

Community
  • 1
  • 1
ZafarYousafi
  • 8,640
  • 5
  • 33
  • 39
  • I think what I will do is use that article. Since I have read it, I don't fully understand it, is what happens this: 1. Program logs into site and retrieves the Cookie. 2. After storing the cookie, it then proceeds to use the cookie as a form of authentication when you wish to download a file or complete an operation on the website. – Vasa Serafin Jul 22 '12 at 02:21