C#FTP Connection Failed

Asked

Viewed 1,255 times

5

I’m developing an application Console in which performs connection to an FTP server (or at least should), I have the following code:

Class Ftpconnection

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

Execution of the Method

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 failure: ftp://127.0.0.1:21
Requested URI is invalid for FTP command.
Unable to connect to informed data...

I am using XAMPP’s Filezilla Server. His connection log when I try a connection is as follows:

(000013)18/05/2015 17:28:07 - (not logged in) (127.0.0.1)> Connected, sending Welcome message...
(000013)18/05/2015 17:28:07 - (not logged in) (127.0.0.1)> 220-Filezilla Server version 0.9.41 beta
(000013)18/05/2015 17:28:07 - (not logged in) (127.0.0.1)> 220-Written by Tim Kosse ([email protected])
(000013)18/05/2015 17:28:07 - (not logged in) (127.0.0.1)> 220 Please visit http://sourceforge.net/projects/filezilla/
(000013)18/05/2015 17:28:07 - (not logged in) (127.0.0.1)> Disconnected.

Using Filezilla Client I normally connect with the same connection data:

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

I don’t know if that method ftp.GetResponse(); is the correct connection test. I am trying to use it to test the connection. If someone knows what’s wrong and can add a connection test code and a real connection example (as I haven’t gotten to that part yet) with sending files, in the reply I thank.

1 answer

3


I solved the connection problem by adding the Request method.

request.Method = WebRequestMethods.Ftp.ListDirectory;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.