开发者

C# - FTP check/test server

I want to test a given IP and login detail to see whether the FTP server is active/details are correct. Nothing more.

I have looked on the web for a solution but none seem simple/eloquent as, for example, using the Ping class to ping and check for a server. I have the following, which doesn't 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 have also tried:

FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create开发者_开发技巧("ftp://172.29.200.158/");

Error message: System.Net.WebException: The requested URI is invalid for this FTP command. at System.Net.FtpWebRequest.GetResponse()

Thanks in advance.


You need to set the Method on the ftp request:

requestDir.Method = WebRequestMethods.Ftp.ListDirectory;


You need to specify the method you are using for the connection, these can be found in the WebRequestMethods.Ftp namespace. By default I believe you will be trying to download a file and your URI doesn't specify a file to download, hence the invalid URI error. Try putting a test file in the root of your FTP site, perhaps called test.txt and then modifying your URI to be ftp://172.29.200.158/test.txt, that should then try to download the file.

It's important to bear in mind that this is an FTP request class and not an FTP client, i.e. it performs specific actions rather than opening a channel to allow you to browse, upload, download, etc.


Why don't you simply treat the Exception in a catch block? ::- ). The code below would correctly determine if an FTP working or not. It relies on the "ListDirectoryDetails" Web Request Method, which should be fast and does not require a lot of bandwidth (normally ::- D, that is, unless you poll a 15k files FTP).

Variables: _URL, _User, _Pass, _PassiveMode (you can try checking on both modes).

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

I forget what is the exact error for a "dead" FTP, but it's not hard to just put a breakpoint in that catch, see what the Exception is and handle it ::- ).

Just throw all this in a function with 4 parameters, or perhaps 3 and you try both Passive & Active internally. The function would return a Boolean, whether or not the FTP is working. There is a bit of waste as you request a file list, but that's only necessary if you're being too thorough. You may remove the "reader" as well as the response stream I think ::- ).

Hope this helps ::- ).


I think you first have to set requestDir.Method to some ftp command before doing GetResponse(). See WebRequestMethods.Ftp for possible commands.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜