3

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.

claco
  • 739
  • 1
  • 8
  • 18

10 Answers10

2

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

Martin Vobr
  • 5,757
  • 2
  • 37
  • 43
1

I have no particular experience but sharptoolbox offer plenty implementations.

Jakub Šturc
  • 35,201
  • 25
  • 90
  • 110
1

You can give "Indy.Sockets" a try. It can handle a lot of high level network protocols, including ftp.

Juanma
  • 3,961
  • 3
  • 27
  • 26
1

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

Brad Bruce
  • 7,638
  • 3
  • 39
  • 60
0

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();
freggel
  • 552
  • 2
  • 6
  • 13
0

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;
}
pinckerman
  • 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
0

I used this in my project recently and it worked great.

http://www.codeproject.com/KB/IP/ftplib.aspxt

Rush Frisby
  • 11,388
  • 19
  • 63
  • 83
0

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.

Kearns
  • 1,119
  • 7
  • 10
0

.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).

Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
Brady Moritz
  • 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
    OP changed the question on us, making my answer obsolete. – Brady Moritz May 05 '12 at 14:26
-2

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794