FTP / FTPS / SFTP之间的区别 – 与其中任何一个的可配置连接

我需要创建一个C#应用程序,根据app.config文件中输入的设置(使用“ftp \ ftps \ sftp”)将excel文件上传到“FTP / SFTP”服务器。

我很满意这些协议,有很多疑问。

  1. FTP和SFTP服务器有什么区别?
  2. 是否可以使用SFTP连接方法访问FTP服务器,反之亦然(指导使用Rebex库连接到SFTP)?
  3. 如何将以下FTP上传方法更改为FTPS

代码如下:

 string PureFileName = new FileInfo(fileName).Name; string uploadUrl = String.Format("ftp://{0}/{1}", ftpurl, PureFileName); FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl); req.Proxy = null; req.Method = WebRequestMethods.Ftp.UploadFile; req.Credentials = new NetworkCredential(user, pass); req.UseBinary = true; req.UsePassive = true; byte[] data = File.ReadAllBytes(fileName); req.ContentLength = data.Length; Stream stream = req.GetRequestStream(); stream.Write(data, 0, data.Length); stream.Close(); FtpWebResponse res = (FtpWebResponse)req.GetResponse(); 

是否就像将url从FTP更改为FTPS?

 string uploadUrl = String.Format("ftps://{0}/{1}", ftpurl, PureFileName); 

  • FTP:旧文件传输协议(RFC959)。 防火墙存在问题,因为它使用动态端口,并且在应用程序级别交换有关这些端口的信息。
  • FTPS:旧的FTP协议,但添加了对TLS的支持。 防火墙问题更加严重,因为这些防火墙无法再查看应用程序级别以找出使用的端口。
  • SFTP:完全不同的东西,因为它使用SSH协议传输文件。 防火墙没有问题。

如果您的代码能够处理FTPS,它通常也能够处理FTP,但是有很多代码只能处理FTP而不能处理FTPS。 由于SFTP是一个完全不同的协议代码处理FTP / FTPS通常无法做SFTP。 而SFTP处理代码不会做FTP / FTPS。 有一些例外,即FileZilla可以在单个应用程序中处理所有这些协议。

至于将FTPS与FtpWebRequests一起使用,请参阅msdn 。 使用SFTP和FtpWebRequests是不可能的,但还有其他库 。

SFTP和FTP / FTPS是两种完全不同的协议。 您无法使用FTP上传到SFTP服务器,反之亦然。 FTPS是FTP over TLS / SSL会话。 大多数FTP客户端/库也支持FTPS。

.NET框架本身仅支持FTP(S)(通过FtpWebRequest类 )。 要使用FTPS,请使用ftps:// URL或将FtpWebRequest.EnableSsl设置为true

.NET框架中没有对SFTP的本机支持。 您必须为SFTP使用第三方库 。


有些库为所有这些协议提供统一的接口。

例如,对于WinSCP .NET程序集 ,它(几乎)仅将SessionOptions.Protocol设置为Protocol.FTPProtocol.SFTP

SFTP协议:

 SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = "example.com", UserName = "user", Password = "mypassword", SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" }; Session session = new Session(); session.Open(sessionOptions); 

FTP协议:

 SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = "example.com", UserName = "user", Password = "mypassword", }; Session session = new Session(); session.Open(sessionOptions); 

FTPS协议:

 SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, FtpSecure = FtpSecure.Explicit, HostName = "example.com", UserName = "user", Password = "mypassword", }; Session session = new Session(); session.Open(sessionOptions); 

如果需要使会话可配置,可以使用SessionOptions.ParseUrl轻松实现,该允许您使用在配置文件中设置的单个连接字符串(URL)配置主要会话选项。

 SessionOptions sessionOptions = new SessionOptions(); sessionOptions.ParseUrl(connectionString); Session session = new Session(); session.Open(sessionOptions); 

连接字符串可以是:

  • SFTP: sftp://user@mypassword;fingerprint=ssh-rsa-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx@example.com
  • FTP: ftp://user@mypassword@example.com
  • FTPS: ftpes://user@mypassword@example.com

您可以让WinSCP(GUI)为您生成URL(连接字符串) 。


请注意,WinSCP .NET程序集不是本机.NET库。 它只是一个围绕控制台应用程序( WinSCP )的瘦.NET包装器。

可能存在本机.NET库,它们支持具有统一接口的所有协议。 但我不知道任何免费的。

(我是WinSCP的作者)

  1. 通过将鼠标hover在您提问的标签上,可以很容易地找到它。 FTP和SFTP是完全不同的协议。 FTPS是带加密的FTP

  2. 没有

  3. 如果您使用的是WebRequestMethods.Ftp.UploadFile; 它不适用于SFTP,也许不适用于FTPS,但必须有一些选项来启用加密。

对于FTPS明确

  // Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = "address.co.za", FtpSecure = FtpSecure.Explicit, UserName = "username", Password = "pass", TlsHostCertificateFingerprint = "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" }; 

如果您需要下载帮助,上传获取文件列表请与我联系