Difference between FTP/FTPS/SFTP - Configurable co

2020-03-04 04:19发布

问题:

I have a requirement like need to create a C# app which will upload an excel file to "FTP/SFTP" server based on settings entered on the app.config file (using "ftp\ftps\sftp").

I am fresh to these protocols, having so many doubts.

  1. What is the difference between FTP and SFTP server?
  2. Is it possible to access FTP server using SFTP connection methods and vice versa (guided to use Rebex library to connect to SFTP)?
  3. How can change following FTP upload method to FTPS

Code is below:

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

Is it like changing url from FTP to FTPS?

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

回答1:

  • FTP: the old file transfer protocol (RFC959). Problematic with firewalls since it is using dynamic ports and the information about these ports gets exchanged at the application level.
  • FTPS: the old FTP protocol but support for TLS added. Even more problematic with firewalls since these can no longer look into the application level to find out which ports are used.
  • SFTP: something completely different, because it uses the SSH protocol to transfer files. No problems with firewalls.

If your code is able to handle FTPS it is usually able to handle FTP too, but there is lots of code which can only handle FTP and not FTPS. Since SFTP is a completely different protocol code handling FTP/FTPS will usually not be able to do SFTP. And SFTP handling code will not do FTP/FTPS. There are exceptions, i.e. FileZilla can handle all these protocols in a single application.

As for using FTPS with FtpWebRequests see msdn. Using SFTP with FtpWebRequests is not possible but there are other libraries.



回答2:

The SFTP and the FTP/FTPS are two completely different protocols. You cannot use the FTP to upload to an SFTP server and vice versa. The FTPS is FTP over TLS/SSL session. Most FTP clients/libraries do support the FTPS as well.

Only the FTP(S) is supported natively by the .NET framework (via the FtpWebRequest class). To use the FTPS, use the ftps:// URL or set the FtpWebRequest.EnableSsl to true.

There's no native support for the SFTP in the .NET framework. You have to use a 3rd party library for the SFTP.


There are libraries that offer an uniform interface to all these protocols.

For example with the WinSCP .NET assembly, it is (almost) only about setting the SessionOptions.Protocol to the Protocol.FTP or the Protocol.SFTP.

SFTP protocol:

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 protocol:

SessionOptions sessionOptions = new SessionOptions {
    Protocol = Protocol.Ftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
};

Session session = new Session();
session.Open(sessionOptions);

FTPS protocol:

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

Session session = new Session();
session.Open(sessionOptions);

If you need to make the session configurable, you can make it easy for you by using the SessionOptions.ParseUrl that allows you to configure major session options using a single connection string (URL) that you set in your configuration file.

SessionOptions sessionOptions = new SessionOptions();
sessionOptions.ParseUrl(connectionString);

Session session = new Session();
session.Open(sessionOptions);

The connection string can be like:

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

You can have WinSCP (GUI) generate the URL (connection string) for you.


Note that the WinSCP .NET assembly is not a native .NET library. It's just a thin .NET wrapper around a console application (WinSCP).

There might be native .NET libraries that support all the protocols with an uniform interface. But I'm not aware of any free one.

(I'm the author of WinSCP)



回答3:

  1. Can be find quite easily by hovering over the tags under your question. FTP and SFTP are totally different protocols. FTPS is FTP with encryption

  2. No

  3. If you are using WebRequestMethods.Ftp.UploadFile; it will not work for SFTP, maybe for FTPS, but there must be some option to turn the encryption on.



回答4:

For FTPS Explicit

  // 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"
            };

If you need help with downloading, uploading of getting file list do contact me



标签: c# ftp sftp ftps