想要使用C#(FtpWebResponse)从FTP读取文件列表,但它返回HTML

发布于 2024-12-26 10:08:39 字数 1401 浏览 2 评论 0原文

我使用下面的代码从 FTP 站点获取文件。它在我的电脑上可以运行,但当我在另一台电脑上运行它时,它只返回HTML代码(当我通过浏览器访问FTP时,我可以看到HTML是网页的代码)。怎么了?

public String GetFilesAsString(string folder,string fileExtension)
{
    StringBuilder result = new StringBuilder();
    FtpWebRequest reqFTP;
    try
    {
        String ftpserver = ftp + folder+"/";

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpserver));
        reqFTP.UsePassive = false;
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(username, password);
        reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

        StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
        string line = "";

        while (reader.Peek()>-1)
        {
            line = reader.ReadLine();
            Console.WriteLine(line);//**********HTML was wrote out here*************
        }

        if (result.ToString().LastIndexOf('\n') >= 0)
            result.Remove(result.ToString().LastIndexOf('\n'), 1);
        reader.Close();
        response.Close();

        return result.ToString();
    }
    catch (Exception ex)
    {
    }
    return null;
}

输入图片此处描述

I use below codes to get files from a FTP site. It works in my computer, but it only return HTML codes when I run it on another computer (I can see that the HTML are codes of web page when I access FTP via browser). What's wrong?

public String GetFilesAsString(string folder,string fileExtension)
{
    StringBuilder result = new StringBuilder();
    FtpWebRequest reqFTP;
    try
    {
        String ftpserver = ftp + folder+"/";

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpserver));
        reqFTP.UsePassive = false;
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(username, password);
        reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

        StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
        string line = "";

        while (reader.Peek()>-1)
        {
            line = reader.ReadLine();
            Console.WriteLine(line);//**********HTML was wrote out here*************
        }

        if (result.ToString().LastIndexOf('\n') >= 0)
            result.Remove(result.ToString().LastIndexOf('\n'), 1);
        reader.Close();
        response.Close();

        return result.ToString();
    }
    catch (Exception ex)
    {
    }
    return null;
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橪书 2025-01-02 10:08:39

会不会是网络代理干扰?尝试使用以下方法绕过代理:

reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();

Could it be a web proxy interfering? Try to get around the proxy by using the following:

reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
无法回应 2025-01-02 10:08:39

这是通过 HTTP 代理使用 FtpWebRequest 的结果。文件列表使用 HTML 标签打印得非常漂亮,这些标签具有指向列表中各个文件的 超链接。

如果您无法绕过代理,在我们的例子中,可以从封闭的

 元素中抓取包含文件内容的部分,并将其加载到 XmlDocument,通过.SelectNodes("//A/text()")拉出文件列表

This is the result of using FtpWebRequest through an HTTP Proxy. The file listing gets pretty printed with HTML tags which have <A> hyperlinks to the individual files in the listing.

If you can't bypass the proxy, in our case it was possible to scrape the section with the file contents out of an enclosing <PRE> element, load it into an XmlDocument, and pull the file list out through .SelectNodes("//A/text()")

尾戒 2025-01-02 10:08:39

我找到了解决方案:默认代理意外启用,

我现在必须使用配置文件专门禁用它:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <system.net>
    <defaultProxy enabled="false" useDefaultCredentials="true"/>
  </system.net>
</configuration>

事实上,这确实是一个 .NET 问题!

I found the solution: the default proxy was enabled unexpectedly

I now have to disable it specifically using a config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <system.net>
    <defaultProxy enabled="false" useDefaultCredentials="true"/>
  </system.net>
</configuration>

In fact, it is really a .NET issue!

花海 2025-01-02 10:08:39

FTP 需要 PassiveMode 来下载、上传...

相反,请尝试使用:

reqFTP.UsePassive = true;

FTP Requires PassiveMode to download, upload...

Instead, try to use:

reqFTP.UsePassive = true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文