atconway
atconway

Reputation: 21304

How do I download a file from an FTP server using FTP over SSL using .NET?

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!

Upvotes: 1

Views: 10471

Answers (3)

Pawel Lesnikowski
Pawel Lesnikowski

Reputation: 6381

If you need some more features like implicit SSL, hash verification, or just cleaner syntax you can use Ftp.dll FTP/FTPS client:

using(Ftp ftp = new Ftp())
{
    ftp.Connect("ftp.server.com");                       
    ftp.Login("user", "password");

    ftp.ChangeFolder("downloads");
    ftp.Download("report.txt", @"c:\report.txt");

    ftp.Close();
}

Please note that this is a commercial product and I'm the author.

Upvotes: 0

atconway
atconway

Reputation: 21304

Here is the final VB.NET code I used:

    Dim request As System.Net.FtpWebRequest = DirectCast(WebRequest.Create(New Uri("ftp://sftp.domain.com/myFile.txt")), System.Net.FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.DownloadFile
    request.EnableSsl = True
    request.Credentials = New Net.NetworkCredential("username", "password")
    request.UsePassive = True
    Dim response As System.Net.FtpWebResponse = DirectCast(request.GetResponse(), System.Net.FtpWebResponse)

Full details here:

Download FTP Files Using FTP Over SSL (SFTP) in .NET:
http://allen-conway-dotnet.blogspot.com/2010/11/download-ftp-files-using-ftp-over-ssl.html

Upvotes: 4

SLaks
SLaks

Reputation: 887453

Like this:

var request = (FtpWebRequest)WebRequest.Create("ftp://...");
request.EnableSsl = true;
using (var response = request.GetResponse()) 
using (var data = response.GetResponseStream()) {
    ...
}

Upvotes: 5

Related Questions