Trying to do error handling when connecting to an ftp server. Disappears this so far:

try { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); request.Credentials = new NetworkCredential(login, password); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); Console.WriteLine(reader.ReadToEnd()); Console.WriteLine("Команда выполнена, статус {0}", response.StatusDescription); reader.Close(); response.Close(); } catch (UriFormatException) { // тут вызов метода для повторного указания uri,login и password } 

How can I check the availability of a successful connection to the server after entering uri and login with a password? While processing an error only if uri is such, for example: string uri = "localhost"; .

1 answer 1

The check should be made immediately after the request is made, that is, after the line

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

Next, your script:

 switch (response.StatusCode) { case FtpStatusCode.AccountNeeded: ... break; case FtpStatusCode.CommandOK: ... break; } 

All possible response statuses, as already noted, can be found here.

  • Thanks for the help. Yesterday I managed to do it all the same, but with a crutch. Now, I read the answer and I understand that the code you offer looks much more elegant than what I did. - 4ezy