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

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



User Comments

Title: Can you help me   
Name: Le Cong Bang
Date: 5/4/2009 3:52:31 AM
Comment:
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: 8/25/2006 7:07:29 AM
Comment:
File not found error is coming
Title: Need VB.Net Code   
Name: Parvati
Date: 8/25/2006 6:57:28 AM
Comment:
Hi,
I neeed vb.net code of ftpview.aspx.cs
Title: How to download   
Name: Poorna
Date: 7/4/2006 7:10:07 AM
Comment:
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: 6/29/2006 4:29:35 AM
Comment:
Thanks, this Article has really saved me a lot of time.
Title: really good   
Name: Nixon Morales
Date: 3/8/2006 8:30:11 AM
Comment:
I really apreciate this article, there is not information or is poor even microsoft site.
Title: eagel   
Name: k
Date: 8/17/2005 9:48:39 AM
Comment:
i need som proxy for crossing from filtered sites
Title: Proxy   
Name: MiKr
Date: 7/11/2005 4:48:45 AM
Comment:
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: 11/10/2004 2:46:35 PM
Comment:
Steve,

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

Thanks!!!
Keep up the good work.
Title: Permissions   
Name: W
Date: 6/21/2004 2:34:43 PM
Comment:
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-2014 ASPAlliance.com  |  Page Processed at 10/22/2014 9:49:38 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search