How To Write Pluggable Protocol to Support FTP in Managed Classes by Using Visual Basic .NET

Article translations Article translations
Article ID: 812404 - View products that this article applies to.
For a Microsoft Visual C# .NET version of this article, see 812409.
This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.IO
  • System.Net
  • System.Net.Sockets
Expand all | Collapse all

On This Page

Summary

This step-by-step article describes how to write pluggable protocol by using classes from the System.Net namespace.

The Microsoft .NET Framework provides a layered, extensible, and managed implementation of Internet services that can be integrated quickly and easily into your applications. The internet application that uses the request-response model can request data from the Internet by using multiple protocols.

The .NET Framework uses specific classes to provide the three pieces of information that are required to access Internet resources through a request-response model: the URI class, the WebResponse class, and the WebRequest class.

This article describes how to use System.Net to write a pluggable protocol to support FTP in managed classes. Pluggable protocols can communicate in both synchronous and asynchronous modes. However, this article discusses only synchronous FTP communication.

The following file is available for download from the Microsoft Download Center:
Collapse this imageExpand this image
Download
Download the VbFtpClient.exe package now.

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591 How to Obtain Microsoft Support Files from Online Services
Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

Note This sample will not work with CERN proxies. For additional information about CERN proxies, click the following article number to view the article in the Microsoft Knowledge Base:
166961 How To FTP with CERN-Based Proxy Using WinInet API

Create the FtpWebResponse and FtpWebRequest Classes

The WebRequest and WebResponse classes are the basis of pluggable protocols. The descendant classes of WebRequest are registered with the WebRequest class to manage the details of making the actual connections to Internet resources.

In the earlier example of vbFtpClient, the FtpWebResponse class inherits from the WebResponse class. You have to override the ContentType and GetResponseStream methods. The ContentType property provides special information that the client requires to identify the type of content that is delivered by the server. The GetResponseStream method returns a data stream from the internet resource.

In the earlier example, the FtpStream class is used to handle the data stream that is delivered to the server together with the request, as shown in the following sample code:
  Public Class FtpWebResponse
        Inherits WebResponse
        Public Overrides Property ContentType() As String
            Get
                'Override
            End Get
            Set(ByVal Value As String)
                'Override
            End Set
        End Property

        Public Overrides Function GetResponseStream() As Stream
          'Override
        End Function
  End Class
Similarly, by using WebRequest you can manage the details of a request to an Internet resource. This sample code uses the FtpWebRequest class that inherits from the WebRequest class. The WebRequest.Create method creates an FTP WebRequest instance based on the URI class that is passed. The instance that is returned is a typecast to the FtpWebRequest class to access protocol-specific properties. Typically, the WebRequest instance provides all the necessary information to formulate a request.

The following sample code demonstrates the methods that are implemented or overridden in the FtpWebRequest class. For more information and implementation, visit the Download Center for the VbFtpClient.exe file that is mentioned earlier in this article.
Public Class FtpWebRequest
        Inherits WebRequest

        Public Overrides Property Method() As String
            Get
                'Override
            End Get
            Set(ByVal Value As String)
                'Override
            End Set
        End Property

        Public Overrides Property Credentials() As ICredentials
            Get
                'Override
            End Get
            Set(ByVal Value As ICredentials)
                'Override
            End Set
        End Property

        Public Overrides Property ConnectionGroupName() As String
            Get
                'Override
            End Get
            Set(ByVal Value As String)
                'Override
            End Set
        End Property

        Public Overrides Property ContentLength() As Long
            Get
                'Override
            End Get
            Set(ByVal Value As Long)
                'Override
            End Set
        End Property

        Public Overrides Property ContentType() As String
            Get
                'Override
            End Get
            Set(ByVal Value As String)
                'Override
            End Set
        End Property


        Public Overrides Property Proxy() As IWebProxy
            Get
                'Override
            End Get
            Set(ByVal Value As IWebProxy)
                'Override
            End Set
        End Property
        Public Overrides Function GetRequestStream() As Stream
           'Override
        End Function
   
        Public Overrides Function GetResponse() As WebResponse
          'Override
        End Function
 End Class


Create VbFtpClient

The vbFtpClient application accesses the information from the Internet by using a request-response model. You can retrieve protocol-specific information by using the WebRequest and WebResponse classes. To retrieve protocol-specific information, register the descendants of WebRequest by using the WebRequest.RegisterPrefix static method. The IWebRequestCreate interface defines the method that WebRequest descendants will use to register with the WebRequest.Create method.

The following sample code demonstrates how to use IWebRequestCreate:
    Public Class FtpRequestCreator
        Implements IWebRequestCreate
        Public Sub New()
        End Sub
        Public Overridable Function Create(ByVal Url As Uri) As WebRequest Implements IWebRequestCreate.Create
            Return New FtpWebRequest(Url)
        End Function
    End Class
The following sample code demonstrates how the RegisterPrefix method is used and how to create a WebRequest instance:
       ' FtpRequestCreator class implements IWebRequestCreate class, which implements Create method.
        Dim Creator As FtpRequestCreator = New FtpRequestCreator()
        WebRequest.RegisterPrefix("ftp:", Creator)
        Dim szUri As String = New String("ftp://localhost")

	' Create WebRequest.
	Dim w As WebRequest = WebRequest.Create(szUri)
The WebRequest.RegisterPrefix method registers the class and notifies the descendants to use the FTP protocol for retrieving the data. After registration, descendants of WebRequest are created by using the WebRequest.Create method with an argument passed as URI. The WebRequest instance exposes properties, such as GetResponse, that control the request to the server and access to the data stream that is sent to the server. The GetResponse method of the WebRequest instance sends the request from the client application to the server that is identified in the URI.
     Dim r As WebResponse = w.GetResponse()
     Dim respstream As Stream = r.GetResponseStream()
     If (respstream.CanRead) Then
            Dim rdr As StreamReader = New StreamReader(respstream)
            Dim resp As String = rdr.ReadToEnd()
            rdr.Close()
            Console.WriteLine(resp)
     End If
 
The GetResponse method returns a WebResponse instance. The WebResponse provides access to the data that is returned by the server in the form of a stream returned by the GetResponseStream method. This stream can be used and modified in an application. You can derive a class from the stream class and override the method, based on application requirements.

For more information, visit the Download Center for the vbFtpClient.exe file that is mentioned earlier in this article.


Steps to Run the VbFtpClient Executable

  1. Download the vbFtpClient.exe file that is mentioned earlier in this article.
  2. Extract the file.
  3. Open the Readme.htm file in the browser.
  4. To read the steps to build the application, click Building the sample.
  5. To read the steps to run the application, click Running the sample.

References

For more information, see the Microsoft .NET Framework SDK documentation, or visit the following MSDN Web site:
Programming Pluggable Protocols
http://msdn.microsoft.com/en-us/library/1f6c88af(vs.71).aspx


The third-party products that are discussed in this article are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, regarding the performance or reliability of these products.

Properties

Article ID: 812404 - Last Review: September 18, 2012 - Revision: 4.0
Applies to
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbdownload kbfile kbweb kbprogramming kbclient kbftp kbhowtomaster KB812404

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com