HTTPS File Downloader

G

Guest

I have been looking all over the web for an example of how to accomplish this. I am trying to download a comma seperated file from a https server. I can't establish the connection - the error reads

System.Net.WebException: The underlying connection was closed: Could not establish secure channel for SSL/TLS. ---> System.ComponentModel.Win32Exception: The function completed successfully, but must be called again to complete the contex

Here is the code to execute the file download

NetworkCredential cred = new NetworkCredential("user", "pass")
ServicePointManager.CertificatePolicy = new CertPolicy()

tr

WebRequest request = WebRequest.Create("https://securesite.com/file.csv")
request.Credentials = cred

WebResponse response = request.GetResponse()
Stream stream = response.GetResponseStream()

StreamReader reader = null
reader = new StreamReader(stream)


catch (Exception ex

this.txtError.Text = ex.ToString()





class CertPolicy: ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
{
return true;
}
}



Any help on this would greatly be appreciated. Also - any books that anyone could reccommend on this type of programming would be appreciated
Thanks - Drew
 
M

Matt

Drew,

Have you been able to get the file just by using your browser and pointing
to the link you specify?

Since you are specifying a certificate policy I am unsure what you are
trying to accomplish. Have you exchanged a cert with your host server?
What are the settings in IIS for the HTTPS virtual directory?

I recently completed a communications package using certs so I will try to
help with your problem but I need the above information if possible.

Thanks,

Matt

Drew said:
I have been looking all over the web for an example of how to accomplish
this. I am trying to download a comma seperated file from a https server.
I can't establish the connection - the error reads:
System.Net.WebException: The underlying connection was closed: Could not
establish secure channel for SSL/TLS. --->
System.ComponentModel.Win32Exception: The function completed successfully,
but must be called again to complete the context
Here is the code to execute the file download:

NetworkCredential cred = new NetworkCredential("user", "pass");
ServicePointManager.CertificatePolicy = new CertPolicy();

try
{
WebRequest request = WebRequest.Create("https://securesite.com/file.csv");
request.Credentials = cred;

WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();

StreamReader reader = null;
reader = new StreamReader(stream);

}
catch (Exception ex)
{
this.txtError.Text = ex.ToString();
}



}
class CertPolicy: ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate
certificate, WebRequest request, int certificateProblem)
{
return true;
}
}



Any help on this would greatly be appreciated. Also - any books that
anyone could reccommend on this type of programming would be appreciated.
 
M

Matt

Drew,

Yes what you want to do can be done. The reason I was asking about the
browser is that I wanted you to make sure it could be retrieved by some form
and not fight an IIS setting.

I have to say I have never seen a HTTPS request cause a login dialog box but
I am sure it's possible. I need you to tell me what your IIS settings are
for the virtual directory where the file is to be retrieved so I can mirror
them in my environment. I will need to play with some code I have to see if
I can get this working then I will be happy to turn it over to you. Is the
site public by chance so I can try to get to it from my workstation?

Thanks,

Matt


Drew said:
I am trying to download the file without having to go through the broswer
everytime. I am essentially trying to make the download automated (about
every mintutes) so I can parse through and extract the proper info. I have
read all over the internet for an example and the code listed in the first
post is what I came up with. Here is what I do to get the info now through
a browser -
1. type in the https://website/file.csv
2. a windows login prompt comes up
3. I input the user/pass
4. the file is downloaded to a place of my choosing

I want the application to handle of this for me. I really have no clue
about the certificate policy - that is from a post I found somehwere else.
I am not sure how/if I have exchanged a cert with host server - how can I
tell. I am completely new to this type of download process.
 
J

Justin Rogers

You should just need to attach NetworkCredential's to your request.
You'll have to hard-code the credentials into your application, which can
be bad, but that is the only way to thwart the login dialog. You are getting
the dialog because settings on your file or IIS don't allow anonymous access
to the CSV file. If hard-coding credentials is out of the question, then you
will have to re-think your security and provide another way for your app to
access the file.
 
M

Matt

Drew,

Sounds strange.

Our email system went down right after I sent you the solution. I will let
you know when it's available.

Is your site public so I could work with you on this?

Thanks,

Matt

Drew said:
Matt -

I tried emailing to you but it was returned. I tried your solution and it
gives the same security error without the second function part. There must
be a way to do this. I found an application called Net Vampire that I can
schedule to do the download every minute. But - I would still like to
figure this out so I could have everything running together.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top