Welcome to MSDN Blogs Sign in | Join | Help

adarsh's blog

Adarsh Khare works at Microsoft. Everything here, though, is his personal opinion and is not read or approved by Microsoft before it is posted. No warranties or other guarantees will be offered as to the quality of the opinions or anything else offered here.
Understanding the basic of FtpWebRequest programming model on .Net frameworks

A long standing complain from user of .Net frameworks was that it doesn't have support for popular ftp protocol. This concern is going to addressed in .Net frameworks 2.0. Recently released Whidbey Beta-1 contains FtpWebRequest, FtpWebResponse classes to support FTP protocol.

WebRequest/ WebResponse Model

.Net frameworks support for FTP protocolo is implemented on top of WebRequest/WebResponse programming model, which was part of previous versions of  .net frameworks. Here are some key differences, later part of the document also provide some code samples to address these differences

a) Uri Based programming model 

Request/response programming model request is based on full Uri, so FtpWebRequest user would use Uri  as compare to traditional filename and directory name based programming. Your target Uri could indicate directory name or filename depending on the ftp method

FTPMethod

Uri

AppendFile, DownloadFile ,DeleteFile, GetDateTimeStamp, UploadFile

 Uri is file name like ftp://myftpserver/dir1/dir2/test.gif

ListDirectory, ListDirectoryDetails, RemoveDirectory, UploadFileWithUniqueName

 Uri indicates directory name like ftp://myftpserver/dir1/dir2

b) All Ftp Connection Management is hidden to users and internal for users of FtpWebRequest and FtpWebResponse, each FtpWebRequest object is independent Ftp request, User should need write code for only Ftp operation he is doing, example to download file

FtpWebRequest request = WebRequest.Create( "ftp://myftpserver/dir1/dir2/test.htmf");

request.Credentials = new NetworkCredential("username","pass");

WebResponse response = request.GetResponse();

Stream dataStream = response.GetResponseStream();

//  Now you can read stream containing the file data

c) Upoloading and downloading the data follows the standard Stream based pattern for .Net frameworks and allow user to use other stream based classes like StreamReader and StreamWriter on top of it as shown below

StreamReader sr = new StreamReader(responseStream, Encoding.ASCII);
   string ss = sr.ReadToEnd();
 

d) Some advanced feature for Ftp Requests like Ftp pver SSL and Ftp requests through http proxy tunneling are very simple to program,

To go via HttpProxy you can just set Proxy property on WebRequest Object like below

request.Proxy = new WebProxy("http://myproxy");  // go through http proxy

request.EnableSSL = true; // Your web request would use SSL channel to talk to ftp server

Note on using Http Proxy on FTPWebRequest: Http proxy is only supported for limited number of ftp methods (mainly to download file only), so if you have IE settings for proxy on your machine you need to explicitly set FtpWebRequest to not use proxy like below

request.Proxy = GlobalProxySelection.GetEmptyWebProxy();

Keep looking at this weblog for more net class library 2.0 features

 

This posting is provided "AS IS" with no warranties, and confers no rights

Posted: Monday, September 13, 2004 4:00 PM by adarshk

Comments

Richard Tallent said:

Probably asking alot here, but it would be nice if some abstration similar to System.IO.Directory.GetFiles() /GetDirectories() / FileInfo was provided that could parse the "dir" results for various common FTP servers automatically. I don't any details for ListDirectory() above, is that the purpose of this method?
# September 13, 2004 8:18 PM

Random .NET Ramblings said:

# September 14, 2004 12:34 AM

adarshk said:

Currently .Net doesn't support this, but I had posted a sample parsing code on a separate blog

http://blogs.msdn.com/adarshk/archive/2004/09/15/230177.aspx
# September 15, 2004 5:35 PM

Anatoly Lubarsky: T-SQL Weblog said:

# January 21, 2005 6:58 AM

Ouaes Jamali said:

I am trying to connect to the FTPS site from behind a proxy and am getting an "System.Net.WebException: SSL cannot be enabled when using a proxy. at System.Net.FtpWebRequest.GetResponse()" The environment that i am using is Windows 2003, .NET 2.0 and the ISA server is being used as a proxy server and the client has Microsoft Firewall Client for ISA Server. Is there an issue when trying to connect to FTPS site from behind a proxy?

# July 9, 2007 3:21 PM

MM said:

Hello,

I am working on building a FTP client, i am trying to use the new .net 2.0 FTP class.

When i send the request for directory listing

ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails

I get the time and date but its not the format i need, i need the MDTM format

So i checked out the

myrequest.Method = WebRequestMethods.Ftp.GetDateTimestamp

which gives me the result i need.

But the problem is i need to send two requests seperately and i have to create another webrequest then send my credentials again.

Is there a workaround? where i create one ftpwebrequest and send my credentials once and get both the directory listing which i need and the datetimestamp.

Thanks

MM

# August 14, 2007 12:53 PM

Network Class Library Team (System.Net) said:

This is a current compile of the team's existing blogs on FtpWebRequest. I am going to update it periodically

# July 25, 2008 9:23 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker