1

I would like to download a file using VB.NET (preferably) or C# via HTTPS.

I have this code to download a file over plain HTTP:

Dim client As WebClient = New WebClient() Dim wp As WebProxy = New WebProxy("[IP number of our proxy server]", [port number of our proxy server]) wp.Credentials = CredentialCache.DefaultCredentials client.Proxy = wp client.DownloadFile("http://sstatic.net/so/img/logo.png", "c:\logo.png") 

This works.

How do I change this code to download a file that is stored on an HTTPS-server? I guess it has something to do with adding credentials or something.

4
  • Just replace http by https in the DownloadFile function. Commented Jan 26, 2010 at 12:30
  • No then I get the following error: "The remote server returned an error: (403) Forbidden." I should somehow be able to give the user name and password. Commented Jan 26, 2010 at 12:51
  • Hi... just landed here from google, didn't actually read the question, but spotted immediately a probable problem in your code: "c:\logo.png", \l is an escape sequence (invalid?) unless it has a @ before or it is "c:\\logo.png" Commented Sep 9, 2010 at 17:17
  • Hi. It's possible the proxy simply does not support proxying HTTPS traffic. Try switching the URL to https and removing the proxy bit. Commented Sep 13, 2012 at 9:22

3 Answers 3

3

You need to add a certificate validator:

// You need to call it only once in a static constructor or multiple times there is no problem ServicePointManager.ServerCertificateValidationCallback = ValidateCertificate; private static bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; } 

In VB:

ServicePointManager.ServerCertificateValidationCallback = AddressOf ValidateCertificate Dim client As WebClient = New WebClient() '... 'Your code Private Shared Function ValidateCertificate(sender As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) As Boolean return True End Function 
Sign up to request clarification or add additional context in comments.

7 Comments

IIRC you should only need to do this if the SSL certificate isn't 100% good
@Marc: Of course, I used to think that the problem was that for iar :)
@iar: Try with this: client.DownloadFile("user:[email protected]/so/img/logo.png", "c:\logo.png")
@MarcosMeli: that doesn't do the trick: "An exception occurred during a WebClient request", and "The URI prefix is not recognized."
@iar the page strips the real address, try with this without spacess client.DownloadFile("https:// user:[email protected]/so/img/logo.png") Cheers
|
2

You just need to point that address to your HTTPS resource and to inform your credential:

client.Credentials = new NetworkCredential("username", "password"); client.DownloadFile("https://your.resource.here", @"localfile.jog") 

You're talking about how to log into a site protected by a HTML form login. I wrote this code sometime ago and you could to adapt it to login into your remote site: Orkut Login Code

Things you need to keep in mind:

  • If that's an ASP.NET site, you need to call it first to get __EVENTTARGET and __EVENTARGUMENT values, as they're required to process your login postback. If it's not, skip this step.
  • You need to identify that names that site uses to fill your username and password
  • You must to add a CookieContainer. It keeps your login cookie, so subsequent calls uses that authenticated context.
  • After all that, you should be able to get your remote resource and to download it

13 Comments

No then I get the following error: "The remote server returned an error: (403) Forbidden." I should somehow be able to give the user name and password.
ah ok, how do you login in that site? its a windows dialog, or do you have a HTML form to fill?
There is a HTML form to fill.
I don't know, it's a third party HTTPS website.
can you please check in that login page HTML source code and try to find a hidden field named __VIEWSTATE ?
|
0

Try something like this

 WebClient wc = new WebClient(); wc.UseDefaultCredentials = false; CredentialCache creds = new CredentialCache(); creds.Add(new Uri(url), "Basic",new NetworkCredential(username, password, domain)); wc.Credentials = creds; wc.Headers.Add(HttpRequestHeader.UserAgent,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;"); //wc.Headers["Accept"] = "/"; wc.DownloadFile(url,localpath); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.