首页 文章

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

提问于
浏览
4

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

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

  • FTP和SFTP服务器有什么区别?

  • 是否可以使用SFTP连接方法访问FTP服务器,反之亦然(指导使用Rebex库连接到SFTP)?

  • 如何将以下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);

4 回答

  • 1
    • FTP:旧文件传输协议(RFC959) . 防火墙存在问题,因为它使用动态端口,并且在应用程序级别交换有关这些端口的信息 .

    • FTPS:旧的FTP协议但添加了对TLS的支持 . 防火墙甚至更成问题,因为它们不再能够查看应用程序级别以找出使用的端口 .

    • SFTP:完全不同的东西,因为它使用SSH协议传输文件 . 防火墙没有问题 .

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

    至于在FtpWebRequests中使用FTPS,请参阅msdn . 使用SFTP和FtpWebRequests是不可能的,但有other libraries .

  • 6

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

    .NET框架本身仅支持FTP(S)(通过FtpWebRequest class) . 要使用FTPS,请使用 ftps:// URL或将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 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) generate the URL (connection string) for you .


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

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

    (我是WinSCP的作者)

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

    • 没有

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

  • 5

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

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

相关问题