【问题标题】:FTP directory listing returned as HTML instead of simple Linux ls outputFTP 目录列表以 HTML 形式返回,而不是简单的 Linux ls 输出
【发布时间】:2015-01-20 14:19:21
【问题描述】:

(与How can I force a C# FtpWebRequest to use a direct IP to get onto the Internet instead of going through an HTTP proxy?相关)

使用 C# 从 FTP 服务器获取目录列表。输出格式为 HTML。必需是未格式化的列表(由 Linux ls 命令返回)。 (尽量避免解析 HTML 来获取文件列表。)

观察:

  • FTP 服务器是客户端运行的 vsftpd。

  • 列出目录时不会出现此问题,例如Filezilla FTP 服务器。

  • 获取目录列表时连接到 vsftpd 服务器的 Filezilla 客户端超时

    Error: Connection timed out Error: Failed to retrieve directory listing

参考下面的代码,会发生以下行为/错误:

  1. 何时 代码中设置的代理服务器 然后 服务器返回 HTML 格式的列表,而不是简单的 ls 输出。

  2. 何时 代理服务器设置为 null 或 WebRequest.DefaultWebProxyGlobalProxySelection.GetEmptyWebProxy()new WebProxy(); 然后: The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

  3. 何时: 代码中未指定代理服务器,并且代理未设置为 null。 然后 The remote server returned an error: (407) Proxy Authentication Required.

问题

  • 如何设置 C# 代码以获取 ls 目录列表而不是 HTML? 或
  • 是否可以在 vsftpd(服务器)端执行任何操作来阻止 HTML 目录列表?

详情:

代码提取

  FtpWebRequest request = WebRequest.Create(uri) as FtpWebRequest;
  request.Method = WebRequestMethods.Ftp.ListDirectory;

  //1. Works but returns HTML
  request.Proxy = new WebProxy("http://xxx.xxx.xxx.xxx:8080",true); 
  request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; 

  //2. Does not work
  //request.Proxy = null;// WebRequest.DefaultWebProxy;// GlobalProxySelection.GetEmptyWebProxy(); // null; //new WebProxy();

  request.Credentials = server.Credential;
  request.KeepAlive = true;
  request.UsePassive = true;

   FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);

    Regex filter = FileUtils.GetRegex(clientSource.FileFilter);

    while (!reader.EndOfStream)
    {
      ProcessFileLine(reader.ReadLine(), filter, files);
    }
    reader.Close();
    response.Close();

HTML 格式的目录列表

<HTML>
<meta http-equiv="Content-Type" content="text-html; charset=UTF-8">
<HEAD>
<TITLE>FTP root at ftp-jhb.saicomvoice.co.za. </TITLE>
</HEAD>
<BODY>
<H1>FTP root at ftp-jhb.saicomvoice.co.za. </H1>
<HR>
<PRE>
12/11/15 04:36PM [GMT]                      &lt;DIR&gt; <A     HREF="/bin/">bin</A>
12/11/15 12:56PM [GMT]                      &lt;DIR&gt; <A HREF="/boot/">boot</A>
02/22/13 12:00AM [GMT]                      &lt;DIR&gt; <A     HREF="/cgroup/">cgroup</A>
12/11/15 03:36PM [GMT]                      &lt;DIR&gt; <A HREF="/dev/">dev</A>
01/19/15 01:32PM [GMT]                      &lt;DIR&gt; <A HREF="/etc/">etc</A>
12/12/15 11:45AM [GMT]                      &lt;DIR&gt; <A HREF="/home/">home</A>
12/11/15 12:51PM [GMT]                      &lt;DIR&gt; <A </PRE>
<HR>
</BODY>
</HTML>

【问题讨论】:

  • 可能是代理将其格式化为 html。

标签: c# ftp


【解决方案1】:

从描述看来,您需要使用 HTTP 代理才能访问 FTP 服务器。代理不会被 FTP 协议访问,只是转发命令,而是被 HTTP 协议访问。然后,代理将为您执行必要的 FTP 命令,并在 HTTP 响应中为您返回结果。这个结果的样子完全取决于代理。由于大多数用户会使用浏览器访问 HTTP 代理,因此 HTTP 代理通常会返回一个带有结果的 HTML 页面,以便用户可以从那里单击以获取相关文件。

总而言之:由于结果完全取决于代理,因此只要您需要使用此特定代理,就无法以不同的方式获得结果。因此,如果有其他使用 FTP 的方法(即没有此 HTTP 代理),最好与您的管理员联系。

【讨论】:

    【解决方案2】:

    我找到了这段代码,它帮助了我:

    request.Proxy = null;
    

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 2011-09-04
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2021-10-23
      • 1970-01-01
      相关资源
      最近更新 更多