AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=253&pId=-1
FTP Access with WebRequest
page
by Steve Sharrock
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 26646/ 29

This example illustrates the WebRequest class extensibility by implementing a simple FTP pluggable protocol. If you've used the WebClient and WebRequest/WebResponse classes, you've probably noticed that they handle HTTP and FILE protocols. FTP may be included in the next release of the Visual Studio. In the meantime you can add FTP support using the built-in extensiblity features of the WebRequest class.

The key ingredients of this example are courtesy of Lance Olsen of Microsoft. He graciously forwarded the source to a sample command-line .NET application that implements WebRequest FTP support. I've done nothing more than use his FtpWebRequest class in an ASP.NET sample, rather than the command-line interface. Lance's original example can be obtained at http://www.gotdotnet.com/userfiles/Feroze/ftpclient.zip.

For more information on peer-to-peer networked applications, see Lance Olsen's great article at http://msdn.microsoft.com/msdnmag/issues/01/02/netpeers/netpeers.asp

Try running the demo first, and then look at some of the source code. You'll need to clear the "Anonymous" checkbox if you have to enter a user name and password to access your FTP site. Set the "Proxy" checkbox if you need to specify a proxy server. For now, the only FTP commands supported in the demo are DIR and GET (although the class code will handle other commands).

Click to run demo now

I'm only going to touch on a few highlights in the source files here. You can view the files now in a separate window, or download FtpView.zip to study or use the files later. The user interface part of the demo simply obtains the FTP site name and FTP command. If a user name and password is required, or if a proxy server must be specified, this information is also gathered. You can view these user interface files now in a separate window.

Click to see FtpView.aspx
Click to see FtpView.aspx.cs

The real meat of this example is the FtpWebRequest class file supplied by Lance Olsen. Please note that the FtpWebRequest class handles most all of the FTP commands; although this sample restricts you to just listing a directory and getting a file. You can view this file now in a separate window.

Click to see FtpWebRequest.cs

When you look at the FtpView.aspx.cs file, you find that all of the work is done in the SubmitBtn_Click method. The string variables required to initialize the FtpWebRequest object are listed first. Values for these variables are gathered from the input controls. The interesting part of this code is where the FtpRequestCreator is used to register the FTP prefix with the WebRequest class.


  It is interesting to see how simple it is to create a WebProxy and its NetworkCredentials. Also see how easy it is to create the NetworkCredentials for the FTP site's user name and password.

if( ProxyChk.Checked && proxy != "" ) 
{
  WebProxy wproxy = new WebProxy( proxy );
  if( proxyUser != "" ) 
   wproxy.Credentials = new NetworkCredential(proxyUser, proxyPwd );
  w.Proxy = wproxy;
}

if( user != "" ) 
{
  w.Credentials = new NetworkCredential( user, pwd );
}

Conclusion

  I developed a strong affinity for the WebClient class when I first used it to screen-scrape with the HTTP protocol. I was pleasantly surprised when I later found I could also get to files on my site's server using the FILE protocol. Now that I can also use FTP to manage remote sites that I maintain auto-magically, I'm thrilled beyond... well, you get the picture.

Downloads

You can download FtpView.zip if you want to use these two files and don't like to type.

Steve Sharrock -   www.AspAlliance.com/shark



©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-24 3:18:33 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search