FTP client for .NET.

Does anyone know a good, hopefully free FTP class for use in .NET that can really work behind an HTTP proxy or FTP gateway? The FtpWebRequest .NET stuff is awful at best, and I really don't want to roll myself here.

+3
source share
10 answers

Our Rebex FTP works with proxies just fine. The following code shows how to connect to FTP using an HTTP proxy (the code is taken from the FTP manual 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 a trial version of www.rebex.net/ftp.net/download.aspx

+2
source
+1
+1

http://sourceforge.net/projects/dotnetftpclient/ , . PASV, . , FTP-, , HTTP- FTP-.

0

, FTPS () - Socks4.

.NET- Starksoftftps. http://starksoftftps.codeplex.com/

:

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();
0

#, FTP HTTP-.

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

System.Net.WebClient can handle ftp URLs and is a little easier to work with. You can also set credentials and proxy information.

-2
source

Source: https://habr.com/ru/post/1696972/


All Articles