有没有其他方法可以在不使用WebRequestMethods.Ftp.ListDirectory方法的情况下测试FTP服



我正在使用第三方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 命令无效。

如果我可以在不使用该方法的情况下做到这一点,请帮助我。

使用WebRequestMethods.Ftp.PrintWorkingDirectory

  • 这是呜;
  • 它的影响很小;
  • 并且应该在任何服务器上工作。

要允许测试凭据,您还需要禁用FtpWebRequest.KeepAlive。请参阅:
FtpWebRequest 在上次使用正确的密码成功连接后忽略了错误的密码

相关内容

最新更新