C # implicitly download file from ftp server without third party library

I am trying to connect to an ftp server implicitly to download a file. I am having trouble establishing a connection. Here is what I am trying.

using (TcpClient client = new TcpClient("ftp://server/", 990))
  using (SslStream sslStream = new SslStream(client.GetStream(), true))
  {
    // Start SSL/TLS Handshake
    sslStream.AuthenticateAsClient("localhost");

    // Setup a delegate for writing FTP commands to the SSL stream
    Action<string> WriteCommand = delegate(string command)
    {
      byte[] commandBytes =
          Encoding.ASCII.GetBytes(command + Environment.NewLine);
      sslStream.Write(commandBytes, 0, commandBytes.Length);
    };

    // Write raw FTP commands to the SSL stream
    WriteCommand("USER user");
    WriteCommand("PASS password");

    // Connect to data port to download the file 
  }

      

What I need:

1) Code for establishing a ftp server connection implicitly

2) Code for uploading a file over this connection

+3


source to share


6 answers


   using (TcpClient client = new TcpClient("ftp://server/", 990))

      

Port 990 is the assigned port number for the management port for FTPS protocol . Which, as you described, is a file transfer method using encryption using TLS or SSL. Hopefully first, there isn't much point in using SSL anymore since the POODLE attack was discovered . Writing your own client for this is unusual, it has certainly been done before.



There are a lot of details in your question as to why you think you need to write your client. So the correct answer will probably be simple, you won't notice that FTPS support is already built into the .NET Framework.

Use the FtpWebRequest.EnableSsl property .

+1


source


SslStream won't work with ftp, only ssl.

You can use the FtpWebRequest class to fetch data over ftp:

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest%28v=vs.110%29.aspx



There are some examples in the above link, one of which is to download ftp file from ftp server:

public static bool DisplayFileFromServer(Uri serverUri)
{
    // The serverUri parameter should start with the ftp:// scheme. 
    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    WebClient request = new WebClient();

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
    try 
    {
        byte [] newFileData = request.DownloadData (serverUri.ToString());
        string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
        Console.WriteLine(fileString);
    }
    catch (WebException e)
    {
        Console.WriteLine(e.ToString());
    }
    return true;
}

      

0


source


using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            Console.WriteLine(reader.ReadToEnd());

            Console.WriteLine("Download Complete, status {0}", response.StatusDescription);

            reader.Close();
            response.Close();  
        }
    }
}

      

Taken from http://msdn.microsoft.com/en-us/library/vstudio/ms229711%28v=vs.100%29.aspx

0


source


You said you didn't want to purchase a third party library to accomplish this, but an FTP client implementation in CodePlex that claims to support FTPS.

The MIT license is licensed, so if you don't want to use a third party dependency, you can view the source code to see if there is any implementation out there that you can use.

0


source


You can use this code:

public void DownloadFromFTP( string ftpLocation, string fileName, string localFolder, string login, string password )
{
    var remoteFilePath = ftpLocation + @"/" + fileName;

    FtpWebResponse response = null;

    try
    {
        var request = (FtpWebRequest)WebRequest.Create( new Uri( remoteFilePath ) );
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential( login, password );

        response = (FtpWebResponse)request.GetResponse();

        var stream = response.GetResponseStream();

        var localPath = string.Format( @"{0}\{1}", localFolder, fileName );

        using( var fs = File.Create( localPath ) )
        {
            stream.CopyTo( fs );
        }
    }
    catch( Exception ex )
    {
        throw ex;
    }
    finally
    {
        if( response != null )
        {
            response.Close();
        }
    }
}

      

0


source


Change the line below and I trust you can connect.

using (TcpClient client = new TcpClient("ftp://server/", 990))

      

Change the line above:

using (TcpClient client = new TcpClient("server", 990))

      

Also try using the default FTP port of 21:

using (TcpClient client = new TcpClient("server", 21))

      

0


source







All Articles