tags:

views:

63

answers:

2

My post title almost states it all: How do I download a file from an FTP server using FTP over SSL using .NET? I have read a bit and there are several 3rd party components to purchase that wrap up this functionality.

The deal is, this is a very specefic need and is not going to grow much, so if downloading a file from an FTP server using FTP over SSL can be done using the .NET Framework (i.e. System.Net namespace or something), then that would be best. I don't need a ton of functionality, but if for some reason coding against a secure FTP server is a nightmare or not doable through the .NET Framework BCL that would be nice to know, as a 3rd party .dll might be best.

Thank you!

+4  A: 

Like this:

var request = (FtpWebRequest)WebRequest.Create("ftp://...");
request.EnableSsl = true;
using (var response = request.GetResponse()) 
using (var data = response.GetResponseStream()) {
    ...
}
SLaks
I need to pass crededntials -> do I use a "request.Credentials = New Net.NetowrkCredential("user", "pass")" ? Also, I was given a host of sftp.domain.com and when using the code you provided it states it is "an invalid URI" (however I can get to it via a FTP/SFTP client application outside of .NET so I know the URI is valid). If I try "sftp://domain.com" I get an "The URI prefix is not recognized" exception. Any ideas on these (2) items?
atconway
I have a feeling SFTP is not supported via the FtpWebRequest class according to this link: http://connect.microsoft.com/VisualStudio/feedback/details/181054/add-support-for-ftps-and-sftp-uris-for-webrequest
atconway
@atconway: Try the URI, `ftp://sftp.domain.com`.
SLaks
That gave me: "The requested URI is invalid for this FTP command."
atconway
@atconway: In order to execute a command, you need a path, not just a domain name. (eg, `ftp://sftp.domain.com/SomeFile`)
SLaks
Yes that got me closer. I am now getting this error: "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." I 'can' get to this file using FTP client software using the same credentials. Any ideas on this?
atconway
@atconway: I don't know; compare the requests in a network monitor. Also, check the path and method.
SLaks