FTP Connection Failure in C #

5

I'm developing a Console application in that makes the connection to an FTP server (or at least should), I have the following code:

FTPConnection Class

class FTPConnection
{
    private Connection con;
    private FtpWebRequest ftp;

    public FTPConnection(Connection c)
    {
        con = c;
    }

    public bool TestConnection(){
        string host = "ftp://" + con.Host;
        if (con.Port != null) host += ":" + con.Port;
        ftp = (FtpWebRequest)FtpWebRequest.Create( host );
        ftp.Credentials = new NetworkCredential(con.User, con.Pass);
        try
        {
            WebResponse response = ftp.GetResponse();
            Util.Success("Conexão realizada com sucesso!");
        }
        catch (WebException e)
        {
            Util.Error("Falha na conexão: " + host);
            Util.Warning(e.Message);
            return false;
        }

        return true;
    }
}

Method Execution

Connection con = new Connection();
con.Host = "127.0.0.1";
con.User = "carlos";
con.Pass = "123123";
con.Port = "21";

FTPConnection FTPc = new FTPConnection(con);
if (!FTPc.TestConnection())
{
    Util.Warning("Não foi possível conectar com os dados informados...");
}
else
{
    Util.Success("Exito no teste de conexão.");
}

The return is always:

  

Connection failed: ftp://127.0.0.1:21
  The requested URI is invalid for the FTP command.   Could not connect to reported data ...

I'm using FileZilla Server from XAMPP. The connection log of it when I try to connect is as follows:

  

(000013) 05/18/2015 17:28:07 - (not logged in) (127.0.0.1) > Connected, sending welcome message ...
  (000013) 05/18/2015 17:28:07 - (not logged in) (127.0.0.1) > 220-FileZilla Server version 0.9.41 beta
  (000013) 05/18/2015 17:28:07 - (not logged in) (127.0.0.1) > 220-written by Tim Kosse ([email protected])
  (000013) 05/18/2015 17:28:07 - (not logged in) (127.0.0.1) > 220 Please visit link
  (000013) 05/18/2015 17:28:07 - (not logged in) (127.0.0.1) > disconnected.

Using FileZilla Client I usually connect with the same connection data:

Host: 127.0.0.1
User: carlos
Pass: 123123
Port: 21

I do not know if this ftp.GetResponse(); method is the correct one for connection testing. I'm trying to use it to test the connection. If anyone knows what is wrong and can add a connection test code and a real connection example (because I have not yet gotten that part) with sending files, in the reply I thank you.

    
asked by anonymous 18.05.2015 / 22:30

1 answer

3

I solved the connection problem by adding the Request method.

request.Method = WebRequestMethods.Ftp.ListDirectory;
    
19.05.2015 / 14:19