C # - FTP Validation / Testing Server

I want to check the IP data and login to see if the FTP server / details is active. Nothing else.

I found a solution on the Internet, but none of them seem simple / eloquent, such as using the Ping class for ping and checking the server. I have the following that does not work:

    static private void testConntecion()
    {
        FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://172.29.200.158/"));
        requestDir.Credentials = new NetworkCredential("dvm_user", "dvm");

        try
        {
            FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
            Console.WriteLine("Cool Beans");
        }

        catch(Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

I also tried:

FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create("ftp://172.29.200.158/");

Error message: System.Net.WebException: The requested URI is not valid for this FTP command. in System.Net.FtpWebRequest.GetResponse ()

Thanks in advance.

+3
source share
4 answers

You need to set the method in the ftp request:

requestDir.Method = WebRequestMethods.Ftp.ListDirectory;
+4
source

, , WebRequestMethods.Ftp. , , URI , , URI. FTP-, , test.txt, URI ftp://172.29.200.158/test.txt, .

, FTP, FTP, .. , , , , ..

+2

catch?:: -). , FTP . - "ListDirectoryDetails", (:: - D, , FTP 15 . ).

: _URL, _User, _Pass, _PassiveMode ( ).

  FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(_URL);
  FtpWebResponse res;
  StreamReader reader;

  ftp.Credentials = new NetworkCredential(_User, _Pass);
  ftp.KeepAlive = false;
  ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  ftp.UsePassive = _PassiveMode;

  try
  {
    using (res = (FtpWebResponse)ftp.GetResponse())
    {
      reader = new StreamReader(res.GetResponseStream());
    }
  }
  catch
  {
    //Handling code here.
  }

, "" FTP, , , Exception :: -).

4 , , 3, . , FTP. , , . "", , , , - -).

, :: -).

+1

, requestDir.Method ftp, GetResponse(). . WebRequestMethods.Ftp .

0
source

Source: https://habr.com/ru/post/1772842/


All Articles