FtpWebRequest pluggable protocol sample
Rebex FtpWebRequest sample
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/package.zip");
// get and read web response
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
Stream local = File.Create("package.zip");
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();
VB.NET
' 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 As WebRequest = WebRequest.Create("ftp://ftp.example.org/files/package.zip")
' get and read web response
Dim response As WebResponse = request.GetResponse()
Dim stream As Stream = response.GetResponseStream()
Dim local As Stream = File.Create("package.zip")
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()