how to ftp using a https address in vb

Chris Jones
Chris Jones used Ask the Experts™
on
hello

I have this vb.net [program and i had it working perfect using plain ftp but the company changed the way they supply the file so now its ftp over https.how would i do that in vb.net
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The ftpwebrequest class has an enablessl property that allows use of SSL over FTP. Please see the below to check if it is what you are after. Also, FTP and HTTPS are different protocols, it's either one or the other and you can't encapsulate FTP  over HTTPS.

FtpWebRequest request = WebRequest.Create(ftp://myftpserver/dir/filename);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.EnableSsl = true; // Here you enabled request to use ssl instead of clear text
WebResponse response = request.GetResponse();

Open in new window


Class info at MSDN
Chris JonesLead Application Web Developer

Author

Commented:
ok i get this error when i do that

Unable to cast object of type 'System.Net.HttpWebRequest' to type 'System.Net.FtpWebRequest'.

This is the httpaddress

https://ets-scorelink.ets.org/edsasftp/TOEFL/TOE6188.TST

That would indicate that you are indeed just using HTTPS with no FTP involved.

Try the following instead

request = WebRequest.Create(https://ets-scorelink.ets.org/edsasftp/TOEFL/TOE6188.TST);
HTTPWebResponse response = request.GetResponse();

Open in new window


If you need to supply credentials then you would have to set the Credentials property of the request before the request.GetResponse line, similar to the below.

request.Credentials = NetworkCredential("Username", "Password");

Open in new window

Chris JonesLead Application Web Developer

Author

Commented:
hmm this is what i have where should i put this cod3e or what code needs to be replaced. Also the  varable link is the URL



Dim FTPRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(line), System.Net.FtpWebRequest)
                    FTPRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
                    FTPRequest.EnableSsl = True
                    Dim response As System.Net.FtpWebResponse = DirectCast(FTPRequest.GetResponse(), System.Net.FtpWebResponse)

Open in new window

Replace all of those lines with the following, you do not need to explicitly enable SSL as WebRequest.Create will do this for you if the URI is https://

Dim HttpRequest As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(line), System.Net.HttpWebRequest)
HTTPRequest.Method = System.Net.WebRequestMethods.Http.Get 'not strictly necessary as Get is the default
Dim response As System.Net.HttpWebResponse = DirectCast(HTTPRequest.GetResponse(), System.Net.HttpWebResponse)

Open in new window

I've just noticed that I've put "line" rather than "link" as the variable in the WebRequest.Create as I copied that section from the comment.
Chris JonesLead Application Web Developer

Author

Commented:
wait it was line in my code as well sorry.
Commented:
Perhaps it would be better to use a scriptable client like Robo-FTP to do this task... it can download the file and do the PGP decryption for you.  Here is a script that would work for the file that you posted:
:loop_top
FTPLOGON "ets-scorelink.ets.org" /servertype=HTTPS /trust=allow
FTPCD "/edsasftp/TOEFL"
RCVFILE "TOE6188.TST"
FTPLOGOFF
PGPDECRYPT "TOE6188.TST" "" /pw="MyKeyPassphrase" /signature=ignore
PAUSE /until=06:30  ;; sleep until 6:30 am tomorrow morning
GOTO loop_top

Open in new window


Of course you would need to import your PGP key into Robo-FTP and then change the password on the PGPDECRYPT line to match the actual password associated with your private key for decryption but otherwise this script is ready to roll.  It runs in a loop with one iteration per day, at 6:30 am but you could change that PAUSE line to anything or use CRON instead if it is more than a day between executions... Either way,if it in a loop like this you can use the Service Installer that comes with Robo-FTP to run the script as a Widnows Service so it is always on waiting for the next exectuion.  The operating system will restart it automatically if is rebooted.

Also, if this is only a small part of a larger VB project you could remove the looping and just have your VB launch it so it executes and then terminates... or if you need finer control, just create an instance of the Robo-FTP COM interface and feed the commands in one at at time.
Chris JonesLead Application Web Developer

Author

Commented:
the Robo-FTP  is a great ideal but more than what i was neding but may use it in another program.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial