Partilhar via


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("https://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

Comments

  • Anonymous
    September 13, 2004
    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?
  • Anonymous
    September 15, 2004
    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
  • Anonymous
    July 09, 2007
    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?
  • Anonymous
    August 14, 2007
    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 listingftp.Method = WebRequestMethods.Ftp.ListDirectoryDetailsI get the time and date but its not the format i need, i need the MDTM formatSo i checked out themyrequest.Method = WebRequestMethods.Ftp.GetDateTimestampwhich 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.ThanksMM
  • Anonymous
    July 25, 2008
    This is a current compile of the team's existing blogs on FtpWebRequest. I am going to update it periodically
  • Anonymous
    July 01, 2009
    The comment has been removed
  • Anonymous
    July 02, 2009
    恋することって怖くないですか?最近ちょっと臆病になってて…そういうの抜きでえっちなことしたくて… lovely-i0709@docomo.ne.jp優しい人がいたらメール待ってます☆
  • Anonymous
    July 03, 2009
    さあ、今夏も新たな出会いを経験してみませんか?当サイトは円助交際の逆、つまり女性が男性を円助する『逆円助交際』を提供します。逆円交際を未経験の方でも気軽に遊べる大人のマッチングシステムです。年齢上限・容姿・経験一切問いません。男性の方は無料で登録して頂けます。貴方も新たな出会いを経験してみませんか
  • Anonymous
    July 07, 2009
    素人ホストでは日頃のストレスを発散したい、もう一度恋がしたい、そういた女性が癒しを求めて登録されています。当サイトは癒やされたい女性・寂しい女性を癒やす男性が集うカップリングサイトです
  • Anonymous
    July 08, 2009
    The comment has been removed
  • Anonymous
    July 09, 2009
    恥ずかしいけどやらしいことしたくてしょうがありません…誰か一緒にしてくれませんか?とりあえず連絡待ってます☆ cute.y.0902@docomo.ne.jp