【问题标题】:Is there any other way to test connection of FTP server without using WebRequestMethods.Ftp.ListDirectory method?有没有其他方法可以在不使用 WebRequestMethods.Ftp.ListDirectory 方法的情况下测试 FTP 服务器的连接?
【发布时间】:2019-08-23 11:14:31
【问题描述】:

我正在使用第三方 FTP 服务器在那里上传文件。但在上传之前,我想实现测试连接功能,以便用户可以测试他是否输入了正确的凭据。

问题是如果用户无权列出目录。因此,即使他有权进行文件上传,我的代码也无法用于测试连接,因为现在我正在使用WebRequestMethods.Ftp.ListDirectory 方法来测试连接并按预期工作。

bool testResult = false;

try
{
    FtpWebRequest request =
        (FtpWebRequest)WebRequest.Create(objVMTestFTPConnectionRequest.FTPAddress);

    request.Method = WebRequestMethods.Ftp.ListDirectory;

    request.Credentials =
        new NetworkCredential(
            objVMTestFTPConnectionRequest.UserName, objVMTestFTPConnectionRequest.Password);

    WebResponse response = request.GetResponse();

    testResult = true;
}
catch (Exception exception)
{               
    throw;
}
return testResult;

当我删除 ListDirectory 方法时出现以下错误

请求的 URI 对于此 FTP 命令无效。

如果我不使用那种方法也能做到这一点,请帮助我。

【问题讨论】:

    标签: c# asp.net .net ftp ftpwebrequest


    【解决方案1】:

    使用WebRequestMethods.Ftp.PrintWorkingDirectory:

    • 现在是noop;
    • 影响较小;
    • 并且应该可以在任何服务器上运行。

    要允许测试凭据,您还需要禁用FtpWebRequest.KeepAlive。见:
    FtpWebRequest ignores wrong password after previous successful connection using correct password

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2018-04-04
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多