简体   繁体   English

如何使用.NET从SSL上的FTP使用FTP服务器从FTP服务器下载文件?

[英]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? 我的帖子标题几乎说明了一切:如何使用.NET通过SSL上的FTP通过FTP服务器从FTP服务器下载文件? 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 (ie System.Net namespace or something), then that would be best. 关键是,这是一个非常特殊的需求,并且不会增长太多,因此,如果可以使用.NET Framework(即System.Net命名空间等)通过SSL上的FTP从FTP服务器下载文件,则那是最好的。 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. 我不需要大量功能,但是如果出于某种原因针对安全的FTP服务器进行编码是一场噩梦,或者无法通过.NET Framework BCL进行操作,那将是一件好事,因为第三方.dll可能是最好的选择。

Thank you! 谢谢!

Like this: 像这样:

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

Here is the final VB.NET code I used: 这是我使用的最终VB.NET代码:

    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: 在.NET中使用SSL上的FTP(SFTP)下载FTP文件:
http://allen-conway-dotnet.blogspot.com/2010/11/download-ftp-files-using-ftp-over-ssl.html http://allen-conway-dotnet.blogspot.com/2010/11/download-ftp-files-using-ftp-over-ssl.html

If you need some more features like implicit SSL , hash verification, or just cleaner syntax you can use Ftp.dll FTP/FTPS client : 如果您需要更多功能,例如隐式SSL ,哈希验证或仅使用更简洁的语法,则可以使用Ftp.dll FTP / FTPS客户端

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. 请注意,这是一种商业产品,我是作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM