More .NET libraries
-
Rebex SFTP
.NET SFTP client
-
Rebex File Transfer Pack
FTP and SFTP together
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Connecting
On this page:
- Connecting to FTP servers
- Connecting to FTP servers using Explicit or Implicit SSL
- Switching to encrypted communication
- Reverting to unencrypted communication
- Enabling/disabling data transfer encryption
- Setting connection options
- Getting info about existing connection
- Getting info about SSL connection
- Checking connection state
Connecting to FTP servers
To connect to an FTP server running on default port 21, use Connect
method.
In this case, a plain (unencrypted) FTP connection will be established.
// create FTP client instance
var ftp = new Rebex.Net.Ftp();
// connect to a server
ftp.Connect(hostname);
' create FTP client instance
Dim ftp = New Rebex.Net.Ftp
' connect to a server
ftp.Connect(hostname)
Connect
method as an additional argument.
Connecting to FTP servers using Explicit or Implicit SSL
To connect to an FTP server with TLS/SSL encryption, use Connect
method with SslMode
argument specified.
SslMode
, see
differences between Explicit and Implicit modes.
// create FTP client instance
var ftp = new Rebex.Net.Ftp();
// connect to a server using explicit TLS/SSL
// (by default, port 21 is used)
ftp.Connect(hostname, SslMode.Explicit);
// connect to a server using implicit TLS/SSL
// (by default, port 990 is used)
//ftp.Connect(hostname, SslMode.Implicit);
' create FTP client instance
Dim ftp = New Rebex.Net.Ftp
' connect to a server using explicit TLS/SSL
' (by default, port 21 is used)
ftp.Connect(hostname, SslMode.Explicit)
' connect to a server using implicit TLS/SSL
' (by default, port 990is used)
ftp.Connect(hostname, SslMode.Implicit)
Server certificate is validated automatically by Rebex FTP, but you can customize the validation process as well.
Switching to encrypted communication
It's also possible to connect using plain (unencrypted) FTP first and upgrade to TLS/SSL later using Secure
method.
// create FTP client instance
var ftp = new Rebex.Net.Ftp();
// connect to a server on default (not-secured) port 21
ftp.Connect(hostname);
// secure connection
ftp.Secure();
// authenticate securely
ftp.Login(username, password);
' create FTP client instance
Dim ftp = New Rebex.Net.Ftp
' connect to a server on default (not-secured) port 21
ftp.Connect(hostname)
' secure connection
ftp.Secure()
' authenticate securely
ftp.Login(username, password)
Calling (plain-mode) Connect
followed by Secure
is equivalent to calling Connect(serverName, SslMode.Explicit)
.
Reverting to unencrypted communication
After authenticating, it's possible to revert an encrypted connection back to unencrypted using the ClearCommandChannel
method.
This is useful when there is an FTP-aware firewall along the way that only functions properly when it can analyze the
FTP communication.
This only reverts the control connection. Data transfers and file listings are still encrypted
(unless SecureTransfers
property has been set to false
).
// create FTP client instance
var ftp = new Rebex.Net.Ftp();
// connect to a server using Explicit SSL
ftp.Connect(hostname, SslMode.Explicit);
// authenticate securely
ftp.Login(username, password);
// keep data transfers secured
// this is the default mode (it's not necessary to set it)
ftp.SecureTransfers = true;
// revert control connection to unencrypted communication
// anyone e.g. firewall can see commands sent
ftp.ClearCommandChannel();
' create FTP client instance
Dim ftp = New Rebex.Net.Ftp
' connect to a server using Explicit SSL
ftp.Connect(hostname, SslMode.Explicit)
' authenticate securely
ftp.Login(username, password)
' keep data transfers secured
' this is the default mode (it's not necessary to set it)
ftp.SecureTransfers = True
' revert control connection to unencrypted communication
' anyone e.g. firewall can see commands sent
ftp.ClearCommandChannel()
Enabling/disabling data transfer encryption
To enable or disable data transfer encryption, use SecureTransfers
property.
// create FTP client instance, connect with SSL, log in
// ...
// turn off data transfer encryption
ftp.SecureTransfers = false;
// transfer files
// ...
' create FTP client instance, connect with SSL, log in
' ...
' turn off data transfer encryption
ftp.SecureTransfers = False
' transfer files
' ...
In non-secure mode, SecureTransfers
property is ignored.
Setting connection options
There are various connection options such as proxies, transfer modes, character sets, FTP extensions, and more.
// create FTP client instance, specify proxy, connect, log in
// ...
// set active mode
ftp.Passive = true;
// set UTF-8 encoding for commands and responses
ftp.Encoding = Encoding.UTF8;
// use large transfer buffers
ftp.Settings.UseLargeBuffers = true;
// disable MLST extension
ftp.EnabledExtensions &= ~FtpExtensions.MachineProcessingList;
// transfer files
// ...
' create FTP client instance, connect, log in
' ...
' set active mode
ftp.Passive = False
' set UTF-8 encoding for commands and responses
ftp.Encoding = Encoding.UTF8
' use large transfer buffers
ftp.Settings.UseLargeBuffers = True
' disable MLST extension
ftp.EnabledExtensions = ftp.EnabledExtensions And Not FtpExtensions.MachineProcessingList
' transfer files
' ...
Getting info about existing connection
Once connected (or authenticated), you can get useful information about the current connection such as server name, port, OS or user name. Don't be afraid - user's password is not stored with the connection.
// create FTP client instance
var ftp = new Rebex.Net.Ftp();
// connect to a server
ftp.Connect(hostname);
// display info
Console.WriteLine("Connected to {0}:{1} ({2}).",
ftp.ServerName, ftp.ServerPort, ftp.GetSystemName());
// authenticate
ftp.Login(username, password);
// display info to the user
Console.WriteLine("Logged on as {0}.", ftp.UserName);
' create FTP client instance
Dim ftp = New Rebex.Net.Ftp()
' connect to a server
ftp.Connect(hostname)
' display info
Console.WriteLine("Connected to {0}:{1} ({2}).",
ftp.ServerName, ftp.ServerPort, ftp.GetSystemName())
' authenticate
ftp.Login(username, password)
' display info to the user
Console.WriteLine("Logged on as {0}.", ftp.UserName)
Getting info about SSL connection
To get information about SSL connection, use TlsSocket
property.
// create FTP client instance
var ftp = new Rebex.Net.Ftp();
// connect to a server
ftp.Connect(hostname, SslMode.Explicit);
// Cipher property contains a lot of information about the current cipher
Console.WriteLine("TLS cipher info: {0}", ftp.TlsSocket.Cipher);
// ServerCertificate property provides access to server certificate
// the first certificate in the chain is the server's certificate
CertificateChain chain = ftp.TlsSocket.ServerCertificate;
Console.WriteLine("Server's certificate info: Subject: {0}, Issuer: {1}",
chain[0].GetSubject(), chain[0].GetIssuer());
' create FTP client instance
Dim ftp = New Rebex.Net.Ftp()
' connect to a server
ftp.Connect(hostname, SslMode.Explicit)
' Cipher property contains a lot of information about the current cipher
Console.WriteLine("TLS cipher info: {0}", ftp.TlsSocket.Cipher)
' ServerCertificate property provides access to server certificate
' the first certificate in the chain is the server's certificate
Dim chain As CertificateChain = ftp.TlsSocket.ServerCertificate
Console.WriteLine("Server's certificate info: Subject: {0}, Issuer: {1}",
chain(0).GetSubject(), chain(0).GetIssuer())
Checking connection state
To check whether a connection is still alive, use GetConnectionState
method.
This will send a simple "read-only" command to the server to ensure the connection is still up and running.
// check connection state
if (!ftp.IsConnected)
{
// notify the user and return
return;
}
' check connection state
If Not ftp.IsConnected Then
' notify the user and return
Return
End If
Back to feature list...