More .NET components

FtpWebRequest pluggable protocol

On this page:

FtpWebRequest support #

.NET Framework provides Uri, WebRequest and WebResponse classes for accessing internet resources through a unified request/response model and includes support for HTTP, FTP and file:// schemes.

Rebex FTP/SSL can be pugged into this model with its FtpWebRequest class, which provides an FTP-specific implementation of WebRequest class. Once registered, WebRequest.Create can be used for accessing files on FTP servers instead of the built-in FTP implementation.

CSharp

// register the component for the FTP prefix
WebRequest.RegisterPrefix("ftp://", Rebex.Net.FtpWebRequest.Creator);

// WebRequest now supports ftp protocol in addition to HTTP/HTTPS
// and local files - the rest of this code snippet is protocol-agnostic

// create web request for the given URI
WebRequest request = WebRequest.Create("ftp://ftp.rebex.net/readme.txt");

// get and read web response
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();

// save the response to the file in local directory
Stream local = File.Create(@"c:\\Mydata\\readme.txt");

byte[] buffer = new byte[1024];
int n;
do
{
    n = stream.Read(buffer, 0, buffer.Length);
    local.Write(buffer, 0, n);
} while (n > 0);

// close both streams
stream.Close();
local.Close();

VisualBasic

' register the component for the FTP prefix
WebRequest.RegisterPrefix("ftp:'", Rebex.Net.FtpWebRequest.Creator)

' WebRequest now supports ftp protocol in addition to HTTP/HTTPS
' and local files - the rest of this code snippet is protocol-agnostic

' create web request for the given URI
Dim request = WebRequest.Create("ftp://ftp.rebex.net/readme.txt")

' get and read web response
Dim response = request.GetResponse()
Dim stream = response.GetResponseStream()

' save the response to the file in local directory
Dim local = File.Create("c:\Mydata\readme.txt")

Dim buffer(1023) As Byte
Dim n As Integer
Do
    n = stream.Read(buffer, 0, buffer.Length)
    local.Write(buffer, 0, n)
Loop While (n > 0)

' close both streams
stream.Close()
local.Close()

This approach may be useful in some cases, but usually it is simpler and more versatile to use the Ftp class designed directly for FTP transfers (and not for HTTP communication).
For instance, the above sample would only use several lines of code if it used the Ftp class:

CSharp

// create an FTP client object
var ftp = new Rebex.Net.Ftp();

// connect and login anonymously
ftp.Connect("ftp.rebex.net");
ftp.Login("anonymous", "");

// download the file and save it to local directory
ftp.GetFile("readme.txt", @"c:\\MyData\\readme.txt");

// disconnect
ftp.Disconnect();

VisualBasic

' create an FTP client object
Dim ftp = New Rebex.Net.Ftp()

' connect and login anonymously
ftp.Connect("ftp.rebex.net")
ftp.Login("anonymous", "")

' download the file and save it to local directory
ftp.GetFile("readme.txt", "c:\MyData\readme.txt")

' disconnect
ftp.Disconnect()