【问题标题】:FtpWebRequest returning "550 File unavailable (e.g. file not found, no access)" when using ListDirectoryDetails for a directory that exists对存在的目录使用 ListDirectoryDe​​tails 时,FtpWebRequest 返回“550 文件不可用(例如,文件未找到,无法访问)”
【发布时间】:2020-10-09 07:24:51
【问题描述】:

我有一个烦人的问题,阻止我在 FTP 中获取我需要的文件。该文件可能有不同的名称,因此我需要先访问该文件夹并列出其中的文件,然后直接对该文件进行请求。

我的问题是,例如,我可以在 Filezilla 中访问此文件,并且也完美地发现了该文件夹,但是当使用 FtpWebResponse 实例获取该文件夹时,出现错误 550

550 文件不可用(例如文件未找到,无法访问)

这里是代码:

FtpWebRequest wr = (FtpWebRequest)WebRequest.Create("ftp://ftp.dachser.com/data/edi/kunden/da46168958/out");

wr.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

wr.Credentials = new NetworkCredential("login", "password");

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

Stream reponseStream = response.GetResponseStream();

StreamReader reader = new StreamReader(reponseStream);

string names = reader.ReadToEnd();

FtpWebResponse 响应 = (FtpWebResponse)wr.GetResponse();

是抛出错误的那一行

PS:生产、测试​​和 FileZilla 在同一个域中,使用相同的互联网连接(如果有帮助)

感谢您的关注和反馈

FileZilla 日志:

来自我的程序的日志,红色圈出的错误与 FTP 错误无关

【问题讨论】:

    标签: c# .net ftp ftpwebrequest


    【解决方案1】:

    FtpWebRequest 解释 URL 时,它不会将分隔主机名和路径的斜线视为路径的一部分。然后将提取的路径与 FTP CWD 命令按原样使用。这意味着 FTP 服务器将相对于您的主目录解析路径。如果您的帐户未经过 chroot(客户端未将 home 视为根),则缺少前导斜杠会导致意外行为。

    在您的情况下,您从/remote/path 开始并使用ftp://example.com/remote/path/ 之类的URL,它将尝试更改为remote/path,因此最终更改为/remote/path/remote/path。这不是你想要的。

    • 您必须使用主文件夹的相对路径。在您的情况下,这意味着使用没有任何路径的 URL。
    • 或者使用绝对路径,需要在主机名后使用两个斜杠:ftp://example.com//remote/path/

    另请注意,文件夹的 URL 应以斜杠结尾:Why does FtpWebRequest return an empty stream for this existing directory?

    其他550问题见FtpWebRequest returns error 550 File unavailable

    【讨论】:

      【解决方案2】:

      In 2021 这适用于我们的 Linux 和 Windows live 从 ftp 服务器读取的框(在 Windows 和 Linux 上)

      注意

      • Windows ftp 上的主文件夹是 web
      • Linux ftp 上的主文件夹是 public_html

      TL;DR;

      • 底线:URL 需要以 / 结尾

      有效:

      ftp://ftp.yourdomain.com.br/public_html/
      ftp://ftp.yourdomain.com.br//public_html/
      ftp://ftp.yourdomain.com.br/web/
      ftp://ftp.yourdomain.com.br//web/
      

      它不起作用:

      ftp://ftp.yourdomain.com.br/public_html
      ftp://ftp.yourdomain.com.br//public_html
      ftp://ftp.yourdomain.com.br/web
      ftp://ftp.yourdomain.com.br//web
      

      用法: //验证目录public_html是否存在

      var url = "/public_html/";
      var result = FtpUtil.DoesDirectoryExists(url,  "ftp://ftp.yourdomain.com.br", "ftp user here", "ftp password here");
      

      static bool DoesDirectoryExists(string directory, string ftpHost, string ftpUser, string ftpPassword) {
                          FtpWebRequest ftpRequest = null;
                          try {
                                  ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + ftpHost + directory));
                                  ftpRequest.Credentials = new NetworkCredential(ftpUser, string ftpPassword);
                                  ftpRequest.UseBinary = true;// optional
                                  ftpRequest.KeepAlive = false;// optional
                                  ftpRequest.UsePassive = true;// optional
                                  ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
              
                                  using (FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse()) {
                                      return true;//directory found
                                  }
                          }
                          catch (WebException ex) {
                                  if (ex.Response != null) {
                                      FtpWebResponse response = (FtpWebResponse)ex.Response;
                                      if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                                          return false;// directory not found.  
                                  }
                                  return false; // directory not found.
                          }
                          finally {
                              ftpRequest = null;
                          }
              }
      

      【讨论】:

        猜你喜欢
        • 2016-03-07
        • 2013-07-02
        • 1970-01-01
        • 1970-01-01
        • 2020-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多