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

Comments (13)

  1. 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?

  2. adarshk says:

    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

  3. Ouaes Jamali says:

    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?

  4. MM says:

    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

    http://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

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

  6. 夏真っ盛り!女の子は開放的な気分で一人エッチしたくてウズウズしてるっ!!貴方は女の子のオナ○ーを見て気分を高めてあげてネ!!もちろん、お手伝いしてもオッケーだよ!!さぁ、今すぐ女の子にアクセスしよっ

  7. メル友募集 says:

    恋することって怖くないですか?最近ちょっと臆病になってて…そういうの抜きでえっちなことしたくて… lovely-i0709@docomo.ne.jp優しい人がいたらメール待ってます☆

  8. 逆円助 says:

    さあ、今夏も新たな出会いを経験してみませんか?当サイトは円助交際の逆、つまり女性が男性を円助する『逆円助交際』を提供します。逆円交際を未経験の方でも気軽に遊べる大人のマッチングシステムです。年齢上限・容姿・経験一切問いません。男性の方は無料で登録して頂けます。貴方も新たな出会いを経験してみませんか

  9. 素人 says:

    素人ホストでは日頃のストレスを発散したい、もう一度恋がしたい、そういた女性が癒しを求めて登録されています。当サイトは癒やされたい女性・寂しい女性を癒やす男性が集うカップリングサイトです

  10. 熟女 says:

    熟女だって性欲がある、貴方がもし人妻とSEXしてお金を稼ぎたいのなら、一度人妻ワイフをご利用ください。当サイトには全国各地からお金持ちのセレブたちが集まっています。女性から男性への報酬は、 最低15万円からと決めております。興味のある方は一度当サイト案内をご覧ください

  11. メル友募集 says:

    恥ずかしいけどやらしいことしたくてしょうがありません…誰か一緒にしてくれませんか?とりあえず連絡待ってます☆ cute.y.0902@docomo.ne.jp