Anyone know of a good, hopefully free FTP class for use in .NET that can actually work behind an HTTP proxy or FTP gateway? The FtpWebRequest stuff in .NET is horrible at best, and I really don't want to roll my own here.
-
I'm curious what is wrong with FTPWebrequest? bugs? Or perhaps it uses IE under the hood (which I assume it does) – Brady Moritz Nov 09 '11 at 05:50
-
There's a whole lot wrong with FtpWebRequest. Proxy support is poor for a start. – Matt Mitchell May 23 '12 at 03:36
10 Answers
Our Rebex FTP works with proxies just fine. Following code shows how to connect to the FTP using HTTP proxy (code is taken from FTP tutorial page).
// initialize FTP client
Ftp client = new Ftp();
// setup proxy details
client.Proxy.ProxyType = FtpProxyType.HttpConnect;
client.Proxy.Host = proxyHostname;
client.Proxy.Port = proxyPort;
// add proxy username and password when needed
client.Proxy.UserName = proxyUsername;
client.Proxy.Password = proxyPassword;
// connect, login
client.Connect(hostname, port);
client.Login(username, password);
// do some work
// ...
// disconnect
client.Disconnect();
You can download the trial at www.rebex.net/ftp.net/download.aspx

- 5,757
- 2
- 37
- 43
I have no particular experience but sharptoolbox offer plenty implementations.

- 35,201
- 25
- 90
- 110
You can give "Indy.Sockets" a try. It can handle a lot of high level network protocols, including ftp.

- 3,961
- 3
- 27
- 26
-
I use it, but it does have some quirks like leaving behind temp files. – Jonathan Allen Oct 06 '08 at 05:48
The best one I've run across is edtFTP.net http://www.enterprisedt.com/products/edtftpnet/overview.html
If offers flexibility you don't get in the built-in classes

- 7,638
- 3
- 39
- 60
I had a simalor issue, create an client for FTPS (explicit) communcation through a Socks4 proxy.
After some searching and testing I found .NET library Starksoftftps. http://starksoftftps.codeplex.com/
Here is my code sample:
Socks4ProxyClient socks = new Socks4ProxyClient("socksproxyhost",1010);
FtpClient ftp = new FtpClient("ftpshost",2010,FtpSecurityProtocol.Tls1Explicit);
ftp.Proxy = socks;
ftp.Open("userid", "******");
ftp.PutFile(@"C:\519ec30a-ae15-4bd5-8bcd-94ef3ca49165.xml");
Console.WriteLine(ftp.GetDirListAsText());
ftp.Close();

- 552
- 2
- 6
- 13
Here is my open source C# code that uploads file to FTP via HTTP proxy.
public bool UploadFile(string localFilePath, string remoteDirectory)
{
var fileName = Path.GetFileName(localFilePath);
string content;
using (var reader = new StreamReader(localFilePath))
content = reader.ReadToEnd();
var proxyAuthB64Str = Convert.ToBase64String(Encoding.ASCII.GetBytes(_proxyUserName + ":" + _proxyPassword));
var sendStr = "PUT ftp://" + _ftpLogin + ":" + _ftpPassword
+ "@" + _ftpHost + remoteDirectory + fileName + " HTTP/1.1\n"
+ "Host: " + _ftpHost + "\n"
+ "User-Agent: Mozilla/4.0 (compatible; Eradicator; dotNetClient)\n" + "Proxy-Authorization: Basic " + proxyAuthB64Str + "\n"
+ "Content-Type: application/octet-stream\n"
+ "Content-Length: " + content.Length + "\n"
+ "Connection: close\n\n" + content;
var sendBytes = Encoding.ASCII.GetBytes(sendStr);
using (var proxySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
proxySocket.Connect(_proxyHost, _proxyPort);
if (!proxySocket.Connected)
throw new SocketException();
proxySocket.Send(sendBytes);
const int recvSize = 65536;
var recvBytes = new byte[recvSize];
proxySocket.Receive(recvBytes, recvSize, SocketFlags.Partial);
var responseFirstLine = new string(Encoding.ASCII.GetChars(recvBytes)).Split("\n".ToCharArray()).Take(1).ElementAt(0);
var httpResponseCode = Regex.Replace(responseFirstLine, @"HTTP/1\.\d (\d+) (\w+)", "$1");
var httpResponseDescription = Regex.Replace(responseFirstLine, @"HTTP/1\.\d (\d+) (\w+)", "$2");
return httpResponseCode.StartsWith("2");
}
return false;
}

- 4,115
- 6
- 33
- 42
-
Builtin framework FtpWebRequest can list directories and download files using HTTP proxy, but it cannot upload. – A. 'Eradicator' Polyakov Dec 03 '13 at 14:38
-
var request = (FtpWebRequest)WebRequest.Create(uri); request.Credentials = FtpCredentials; request.Proxy = new WebProxy(_proxyHost, _proxyPort) { Credentials = new NetworkCredential(_proxyUserName, _proxyPassword) }; request.UsePassive = true; using (var resp = request.GetResponse()) using (var stream = resp.GetResponseStream()) ....... – A. 'Eradicator' Polyakov Dec 03 '13 at 14:39
I have used http://sourceforge.net/projects/dotnetftpclient/ for quite a while now, and it does the job nicely. If you use a PASV connection, you shouldn't have any firewall issues. Not sure what an FTP gateway is, but I don't see how the HTTP Proxy would affect any FTP connection.

- 1,119
- 7
- 10
.Net 4.0+ now includes an ftp client class- see this msdn link for more info.
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx
I see options for even using PASV mode etc, so it appears to be fully functional (or so I hope).

- 40,943
- 35
- 118
- 185

- 8,624
- 8
- 66
- 100
-
This class is the one the OP originally describes (rather accruately) as "horrible". This class isn't new (it's been around since .NET 2.0) and doesn't support proxies in any useful manner. – Matt Mitchell May 04 '12 at 03:30
-
1
System.Net.WebClient can handle ftp urls, and it's a bit easier to work with. You can set credentials and proxy information with it, too.

- 399,467
- 113
- 570
- 794
-
No, he said FTPWebRequest was not an option. System.Net.WebClient is a different class entirely. – Joel Coehoorn Sep 16 '08 at 15:34
-
I see upload and download in WebClient but no delete. Is it really possible to delete and rename on a FTP via an HTTP proxy with WebClient? – Jonas Elfström Jun 02 '09 at 13:47
-
Pretty sure webclient uses FtpWebRequest under the hood in any case. – Matt Mitchell May 23 '12 at 03:36