FTP目录列表返回为HTML而不是简单的Linux ls输出 [英] FTP directory listing returned as HTML instead of simple Linux ls output

查看:230
本文介绍了FTP目录列表返回为HTML而不是简单的Linux ls输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(相关于我如何强制C#的FtpWebRequest使用直接IP获取到互联网,而不是通过HTTP代理回事?



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



观察:




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


  • 在列出目录时,不会出现这个问题。 Filezilla FTP服务器。


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



    错误:连接超时
    错误:无法检索目录列表




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





  1. 代理服务器中所设置的码
    ,则
    服务器返回HTML而不是简单LS输出格式化的列表中。



  2. 代理服务器设置为NULL或 WebRequest.DefaultWebProxy OR GlobalProxySelection.GetEmptyWebProxy() OR 新WebProxy();
    ,则:
    远程服务器返回一个错误:(550)文件不可用(例如,未找到文件,没有访问)


  3. 当:
    中的代码和代理未指定代理服务器未设置为空。
    ,则
    远程服务器返回一个错误:(407)代理身份验证




  4. 问题




    • 如何设置的C#代码来获取LS目录列表和不是HTML?

    • 有没有什么可以vsftpd的(服务器)端进行,以防止HTML目录列表?



    详细说明:



    代码提取

     的FtpWebRequest请求= WebRequest.Create(URI)作为的FtpWebRequest; 
    request.Method = WebRequestMethods.Ftp.ListDirectory;

    // 1。作品,但返回HTML
    request.Proxy =新WebProxy( http://xxx.xxx.xxx.xxx:8080,真正的);
    request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // 2。不工作
    //request.Proxy = NULL; // WebRequest.DefaultWebProxy; // GlobalProxySelection.GetEmptyWebProxy(); // 空值; //新的WebProxy();

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

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

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

    ,而(reader.EndOfStream!)
    {
    ProcessFileLine(reader.ReadLine(),过滤器,文件);
    }
    reader.Close();
    response.Close();



    目录列表格式为HTML



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


    解决方案

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



    在总结:由于结果完全依赖于代理也没有办法,只要你需要使用这个特定的代理获得的结果以不同的方式。所以最好的办法是与你的管理员核对是否有另一种使用FTP的方式,即没有这个HTTP代理。


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

    Using C# to get a directory listing from an FTP server. The output is formatted as HTML. Required is an un-formatted list (as returned by the Linux ls command). (Trying to avoid parsing HTML to get list of files.)

    Observations:

    • FTP server is vsftpd run by the client.

    • The problem does not occur when listing a directory on e.g. Filezilla FTP server.

    • Filezilla Client connected to the vsftpd server times out when getting directory listing

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

    Referring to the code below, the following behaviour / errors happen:

    1. WHEN Proxy server set in the code THEN server returns the list formatted in HTML instead of simple ls output.

    2. WHEN Proxy server set to null OR WebRequest.DefaultWebProxy OR GlobalProxySelection.GetEmptyWebProxy() OR new WebProxy(); THEN: The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

    3. WHEN: No proxy server specified in the code AND proxy is NOT set to null. THEN The remote server returned an error: (407) Proxy Authentication Required.

    Questions

    • How do I setup the C# code to get the ls directory listing and not HTML? OR
    • Is there anything that can be done on the vsftpd (server) side to prevent HTML directory listing?

    Details:

    Code extract

      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();
    

    Directory listing formatted as 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>
    

    解决方案

    From the description it looks like that you are required to use a HTTP proxy to access the FTP server. The proxy will not be accessed by the FTP protocol and just forward commands, but instead it will be accessed by the HTTP protocol. The proxy then will do the necessary FTP commands for you and return the result for you inside the HTTP response. How this result will look like depends fully on the proxy. And since most users will access a HTTP proxy using a browser, HTTP proxies usually return a HTML page with the result so that the user can from there just click to get the relevant files.

    In summary: since the result depends fully on the proxy there is no way to get the result in a different way as long as you need to use this specific proxy. So the best would be to check with your administrators if there is another way to use FTP, i.e. without this HTTP proxy.

    这篇关于FTP目录列表返回为HTML而不是简单的Linux ls输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆