FtpWebRequest pluggable protocol sample (.NET Compact Framework)

Rebex FtpWebRequest sample for .NET Compact Framework

The .NET Framework introduced Uri, WebRequest and WebResponse classes for accessing internet resources through a request/response model and includes support for HTTP protocol and file:// scheme to request local files.

Rebex FTP for .NET fits nicely into this model with FtpWebRequest class, which provides an FTP-specific implementation of WebRequest class. Once registered, WebRequest.Create can be used for accessing files on FTP servers in addition to natively supported HTTP and HTTPS.

C#

// 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.example.org/files/readme.txt");

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

// process the stream - for example read the text file
StreamReader reader = new StreamReader(stream);
string textFile = reader.ReadToEnd();

// close the stream
reader.Close();

VB.NET

' register the component for the FTP prefix
WebRequest.RegisterPrefix("ftp:'", 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 As WebRequest = WebRequest.Create("ftp://ftp.example.org/files/readme.txt")

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

' process the stream - for example read the text file
Dim reader As StreamReader = New StreamReader(stream)
Dim textFile As String = reader.ReadToEnd()

' close the stream
reader.Close()