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

时间:2015-10-11 08:02:05

标签: c# ftp sftp ftps

我有一个要求就是需要创建一个C#应用程序,它会将一个excel文件上传到" FTP / SFTP"服务器基于app.config文件中输入的设置(使用" ftp \ ftps \ sftp")。

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

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

    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);
    

4 个答案:

答案 0 :(得分:6)

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

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

至于将FTPS与FtpWebRequests一起使用,请参阅msdn。无法使用SFTP和FtpWebRequests,但有other libraries

答案 1 :(得分:5)

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

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

.NET框架中没有SFTP的本机支持。您必须使用3rd party library for the SFTP

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

例如,使用WinSCP .NET assembly,(几乎)仅将SessionOptions.Protocol设置为Protocol.FTPProtocol.SFTP

SFTP协议:

SessionOptions sessionOptions = new SessionOptions {
    Protocol = Protocol.Sftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
    SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
};

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-xxxxxxxxxxx...=@example.com
  • FTP:ftp://user@mypassword@example.com
  • FTPS:ftpes://user@mypassword@example.com

您可以拥有WinSCP (GUI) generate the URL (connection string) for you

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

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

(我是WinSCP的作者)

答案 2 :(得分:1)

  1. 将鼠标悬停在问题下的标记上可以轻松找到。 FTP和SFTP是完全不同的协议。 FTPS是带加密的FTP

  2. 没有

  3. 如果您使用的是WebRequestMethods.Ftp.UploadFile;,它将无法用于SFTP,也可能用于FTPS,但必须有一些选项可以启用加密。

答案 3 :(得分:0)

对于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"
            };

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