views:

576

answers:

2

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.

+2  A: 
Rubens Farias
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.
iar
ah ok, how do you login in that site? its a windows dialog, or do you have a HTML form to fill?
Rubens Farias
There is a HTML form to fill.
iar
it's an asp.net application?
Rubens Farias
I don't know, it's a third party HTTPS website.
iar
can you please check in that login page HTML source code and try to find a hidden field named `__VIEWSTATE` ?
Rubens Farias
There is no hidden field __VIEWSTATE. The only hidden fields are form_build_id and form_id.
iar
so, isn't asp.net; take a look into my updated answer and skip first step
Rubens Farias
@Rubens Farias: Thanks for your help so far. I'm trying to figure it out using your Orkut Login Code, however I have no clue on how to convert the following piece of code from C# to VB.NET. I hope you can help me out: byte[] buffer = Encoding.UTF8.GetBytes( String.Join(" })));
iar
Solve a problem at time; try to run in c# first and, later, translate to VB.NET
Rubens Farias
You are right. I'm not familiar with C#, and also rather new at VB.NET, so I decided to go with VB. Anyway, when I paste your code in C#, it's the same line of code that cannot be compiled. I'm using Visual Studio Professional 2010 Beta 2. I tried to upload a screenshot but our network policy does not allow me to.
iar
Please take a look at this screenshot, see my above message.http://img11.imageshack.us/img11/3748/codehb.png
iar
@Rubens Farias: I am now able to login to the HTTPS-site by using your Orkut code with some modifications. At the end I'm able to retreive the HTML source of the page after the login form. On that page the link to the download is available. Now how do I go about "clicking" the link, or better: to actually download the file behind that link?
iar
You can to use `DownloadData` or `DownloadFile` methods
Rubens Farias
+1  A: 

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
MarcosMeli
IIRC you should only need to do this if the SSL certificate isn't 100% good
Marc Gravell
But where is the code to submit our username and password?
iar
@Marc: Of course, I used to think that the problem was that for iar :)
MarcosMeli
@iar: Try with this:client.DownloadFile("http://user:[email protected]/so/img/logo.png", "c:\logo.png")
MarcosMeli
@MarcosMeli: that doesn't do the trick: "An exception occurred during a WebClient request", and "The URI prefix is not recognized."
iar
@iar the page strips the real address, try with this without spacessclient.DownloadFile("https:// user:[email protected]/so/img/logo.png")Cheers
MarcosMeli
It gives this error message: The remote server returned an error: (403) Forbidden.
iar
what happend if you paste the full address in a navigator ? the file downloads ? if so the code must work, if not so the pass or user maybe is wrong
MarcosMeli
You solved the problem ?
MarcosMeli