FTP Access with WebRequest
page 1 of 1
Published: 03 Nov 2003
Unedited - Community Contributed
A sample program that illustrates the extensibility of the WebRequest class to implement an FTP pluggable protocol.
by Steve Sharrock
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 26511/ 24

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 );


  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.


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

User Comments

Title: Can you help me   
Name: Le Cong Bang
Date: 2009-05-04 3:52:31 AM
Hi, i need referent ftp client + asp.net. Can you send for me code of link http://authors.aspalliance.com/shark/articles/FtpView/FtpView.aspx.
Thanks you very much.
Thanks again.
My email: bangct01@yahoo.com
Title: File not found erro   
Name: ABC
Date: 2006-08-25 7:07:29 AM
File not found error is coming
Title: Need VB.Net Code   
Name: Parvati
Date: 2006-08-25 6:57:28 AM
I neeed vb.net code of ftpview.aspx.cs
Title: How to download   
Name: Poorna
Date: 2006-07-04 7:10:07 AM
How to download a file with this?

Its saying Invalid directory
I tried by giving absolute and relative paths with get command
Pls help
Title: Good one   
Name: pe4uk
Date: 2006-06-29 4:29:35 AM
Thanks, this Article has really saved me a lot of time.
Title: really good   
Name: Nixon Morales
Date: 2006-03-08 8:30:11 AM
I really apreciate this article, there is not information or is poor even microsoft site.
Title: eagel   
Name: k
Date: 2005-08-17 9:48:39 AM
i need som proxy for crossing from filtered sites
Title: Proxy   
Name: MiKr
Date: 2005-07-11 4:48:45 AM
Is there some simply way how to change code for ISA proxy (HTTP proxy with FTP support , not user@hostname) ?
Title: FTP Access with Web Request   
Name: Jim Pauciello
Date: 2004-11-10 2:46:35 PM

Is there a way where we could change the transfer mode to binary?
Title: FTP Access with WebRequest   
Name: Tarun Upadhyaya
Date: 2004-11-08 4:51:50 AM
Good Work!!!, The article has helped me create a Web based FTP tool , like the one created by dart.

Keep up the good work.
Title: Permissions   
Name: W
Date: 2004-06-21 2:34:43 PM
To use FTPView, what permissions have to be set on the server hosting the aspx file? I continually recieve a "file not found" error that I'm convinced is permission related.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

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