|
| Implement FTP using .NET FCL |
|
|
|
|
| Messages |
|
Related Types |
This message was discovered on microsoft.public.dotnet.framework.aspnet.webservices.
| Jevelson |
Hello, I wish to use FTP file transfer mechanism.I am writting ..NET class library in C#. I found that Prefix of FTP need to be registered.I wish to find.What all steps I have to follow to make FTP file access using c# FCL classes. Thanks, Jevelson
|
|
|
| |
|
| |
| |
| David Qiu |
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 in a protocol-agnostic manner. The .NET Framework uses specific classes to provide the three pieces of information required to access Internet resources through a request - response model namely, URI Class, WebResponse and WebRequest classes. This article details how to use System.Net to write a pluggable protocol to support FTP in managed classes. Pluggable protocols can communicate in synchronous and asynchronous modes.
Create FtpWebResponse and FtpWebRequest Class --------------------------------------------- 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 FtpClient, the FtpWebResponse class inherits from WebResponse class. You have to override ContentType and GetResponseStream methods. The ContentType property provides special information required by the client to identify the type of content delivered by the server. The GetResponseStream method returns a data stream from the internet resource. In the earlier example, FtpStream class is used to handle the data stream delivered to the server along with the request. The following sample code gives an idea how to accomplish this:
public class FtpWebResponse : WebResponse { public override String ContentType { get {/* override */} set { /* override */ } } public override Stream GetResponseStream() { /* override */ } } Similarly, using WebRequest you can manage the details of a request to an Internet resource. The sample uses the FtpWebRequest class that inherits from WebRequest class. The WebRequest.Create method creates FTP WebRequest instance based on the URI passed. The instance thus returned is a typecast to FtpWebRequest class to access protocol-specific properties. In most cases, the WebRequest instance provides all the necessary information to formulate a request. The following details gives you an idea about the methods implemented or overridden in FtpWebRequest class. For detailed information and implementation, visit the ftpclient.exe link as mentioned in the symptoms section:
public class FtpWebRequest : WebRequest { public override String Method {/* override */ } public override ICredentials Credentials { /* override */ get {} /* override */ set {} } public override string ConnectionGroupName { /* override */ get {} /* override */ set {} } public override long ContentLength { /* override */ get {} /* override */ set {} } public override string ContentType { /* override */ get {} /* override */ set {} } public override IWebProxy Proxy { /* override */ get {} /* override */ set {} } public override Stream GetRequestStream() {/*override*/} public override WebResponse GetResponse() {/*override*/} }
Create FtpClient ---------------- The FtpClient application access the information from the Internet using a request - response model. You can retrieve Protocol Specific information using WebRequest and WebResponse classes. To retrieve protocol specific information, you should register the webrequest descendants using WebRequest.RegisterPrefix static method. The IWebRequestCreate interface defines the method that WebRequest descendants should use to register with the WebRequest.Create method. The following sample code illustrates how to use IWebRequestCreate class:
public class FtpRequestCreator : IWebRequestCreate { public FtpRequestCreator() { } public WebRequest Create(Uri Url) { return new FtpWebRequest(Url); } }
The following code demonstrates the usage of RegisterPrefix method and to create a WebRequest instance.
// FtpRequestCreator class implements IWebRequestCreate class, which implement Create method FtpRequestCreator Creator = new FtpRequestCreator(); WebRequest.RegisterPrefix("ftp:", Creator); String szUri= new String("ftp://localhost"); // Create WebRequest WebRequest w = WebRequest.Create(szUri);
The WebRequest.RegisterPrefix method Registers the class and notify the descendants to use FTP protocol for retrieving the data. After registration, webRequest descendants is created using WebRequest.Create method with an argument passed as URI. The WebRequest instance exposes properties such as GetResponse that controls the request to the server and access to the data stream sent to the server. The GetResponse method of WebRequest instance sends the request from the client application to the server identified in the URI.
WebResponse r = w.GetResponse(); Stream respstream = r.GetResponseStream(); if(respstream.CanRead) { StreamReader rdr = new StreamReader(respstream); String resp = rdr.ReadToEnd(); rdr.Close(); Console.WriteLine(resp); } The GetResponse method return a WebResponse instance. The WebResponse provides access to the data 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 stream class and override the method based on application requirements.
Note: This sample will not work with CERN proxies. For additional information about CERN proxies, click the following article number 166961 to view the article 166961 in the Microsoft Knowledge Base:
166961.KB.EN-US: HOWTO: FTP with CERN-Based Proxy Using WinInet API
REFERENCES ========== For more information, please refer to the Microsoft .NET Framework SDK Documentation or MSDN on-line help.
Programming Pluggable Protocols http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm l/cpconprogrammingpluggableprotocols.asp
For more information on the given sample and other ftpclient related samples, please click on the following link: http://www.gotdotnet.com/userarea/filedetails.aspx?FileName=ftpclient.zip
This posting is provided "AS IS" with no warranties, and confers no rights.
David Microsoft Developer Support
|
|
|
| |
|
| |
|
| | |
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|