5,699,431 members and growing! (24,523 online)
Email Password   helpLost your password?
General Programming » Internet / Network » FTP     Intermediate

C# FTP client, based on the WinInet.dll API

By Peter Beedell

The WinInet.dll API is able to tunnel through firewalls - this simple FTP client will support file transfer even when your firewall has only port 21open!
C#, Windows, .NET 1.1, .NET, Visual Studio, Dev

Posted: 16 Sep 2003
Updated: 16 Sep 2003
Views: 118,442
Bookmarked: 30 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
29 votes for this Article.
Popularity: 4.74 Rating: 3.24 out of 5
6 votes, 20.7%
1
3 votes, 10.3%
2
1 vote, 3.4%
3
7 votes, 24.1%
4
12 votes, 41.4%
5

Introduction

When I needed to add automated file transfer to and from a remote FTP server, I found any number of excellent, clear, well structured and beautifully documented examples; both commercial and private - and all useless to me! Our Internet access is over a rather 'well throttled' firewall that seems to only have port 21 left open. The classic sockets approach to implementing an FTP client will open a 'command channel' over port 21 (default FTP port), allowing the client to sign-on etc., but as soon as any data is required to flow a second, 'data channel' must be opened (default port 20). Active or passive, I could not establish a second channel through our firewall.

I would have given up at this stage and started to look for a new employer, when it occurred to me that the Windows Explorer was perfectly able to exchange data with all and any FTP server I pointed it at... this runs over exactly the same firewall, so something strange and not just a bit magical is happening here. It turns out this bit of magic is to be found in the WinInet.dll. I am still not quite sure how the WinInet.dll works, it seems to sweet talk the firewall into opening just a small chink in its armor - but the important thing is that it works!

In order to use the FTP features of WinInet.dll, I needed to encapsulate the API in C#. This was mostly pain free, except for the marshaling of string parameters - it is documented, but you have to know what to look for. Having finished this, I figured I could offer it to the C# community; maybe I will save somebody many frustrating hours trying to bore through his corporate firewall, but also because I am quite sure I am going to get a bunch of really helpful feedback... for example, why does a successful file deletion return error code 2 = file not found? Ah, the mysteries of API programming.

I have included a very minimal console application to test the basic functionality - the default URL is ftp://ftp.microsoft.com/, users are invited to replace this with their own target.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Peter Beedell


As a Brit who sacrificed warm beer baked beans for Bayerisch lager and Bratwurst about 12 years ago, I feel European and proud of it!
Occupation: Web Developer
Location: Germany Germany

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 37 (Total in Forum: 37) (Refresh)FirstPrevNext
GeneralA comfortable FTP class in .NETmemberElmue13:22 27 Aug '08  
QuestionPossibility to use QUOTE RCMD SBMJOB CMD()?memberpaoloden1:58 6 Jan '07  
Generalget every file of a directorymemberwilco968:22 23 Feb '06  
AnswerRe: get every file of a directorymemberantgao5:21 9 Nov '06  
GeneralRe: get every file of a directorymembergan.gary0:39 16 Sep '08  
Generalimportant change required for using in .Net 2.0...memberTim_Mackey3:55 8 Feb '06  
GeneralRe: important change required for using in .Net 2.0...membershareeef9:52 8 Dec '06  
GeneralRe: important change required for using in .Net 2.0...memberVictortto14:08 16 Oct '07  
GeneralRe: important change required for using in .Net 2.0...memberTriple-S1:52 21 Oct '07  
RantRe: important change required for using in .Net 2.0...memberHAIXJ2:09 28 May '08  
GeneralBugmemberelbertlev7:52 8 Jan '06  
GeneralHow to put it in passive modesussadamloving8:56 28 Sep '05  
GeneralfunIConnect failed, error value: 3memberjklpq4:14 23 Mar '05  
GeneralRe: funIConnect failed, error value: 3memberDCChristopher6:20 29 Sep '05  
GeneralRe: funIConnect failed, error value: 3memberMember 291684713:03 26 Feb '08  
GeneralConnection Timeout ProblemmemberRattan20:41 25 Nov '04  
GeneralOverwrite locked filememberkholland155:35 22 Nov '04  
GeneralFree, open-source pure C# FTP componentsussHans Andersen10:15 11 Nov '04  
GeneralRe: Free, open-source pure C# FTP componentmembermaheshsingh21@yahoo.co.in20:39 1 Jul '05  
GeneralNLST using FtpCommandmemberRattan0:36 27 Sep '04  
GeneralRe: NLST using FtpCommandmemberMiataMan9:20 11 Nov '04  
GeneralRe: NLST using FtpCommandmemberJaze5:12 28 Sep '05  
GeneralAn Asembly Code for sending Data to servermemberZeeshan_lqp11:46 10 Sep '04  
Generalwin 98 / 2000 supported?memberTim_Mackey3:56 17 Aug '04  
GeneralRe: win 98 / 2000 supported?memberTim_Mackey5:12 19 Aug '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Sep 2003
Editor: Smitha Vijayan
Copyright 2003 by Peter Beedell
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project