Paul Alexander
Paul Alexander

Reputation: 2748

Reduce time waiting for FTP connection with FtpWebRequest

I'm testing the connection to an FTP server from my c# .NET application which is working. If a connection can be made or the server address is invalid then the response is instant. However, it is very slow if the credentials are valid but no connection can be made. How can I reduce the timeout time on this?

FTP test code:

try
{               
    FtpWebRequest ftpRequest =
        (FtpWebRequest)WebRequest.Create(new Uri("ftp://"+ftpServer+"/"));
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
    ftpRequest.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
    ftpRequest.GetResponse();

    MessageBox.Show("OK");
}
catch (Exception ex)
{
    MessageBox.Show("Error");
}

Thanks

Upvotes: 1

Views: 1336

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202594

Specify a timeout using FtpWebRequest.Timeout.


Or use an asynchronous request using WebRequest.GetResponseAsync.

Then, you can control, how long you wait for an asynchronous response any way you like.

Upvotes: 2

Related Questions