想使用 C# (FtpWebResponse) 从 FTP 读取文件列表,但它返回 HTML [英] Want to use C# (FtpWebResponse) to read file list from FTP, but it returns HTML

查看:19
本文介绍了想使用 C# (FtpWebResponse) 从 FTP 读取文件列表,但它返回 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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('
') >= 0)
            result.Remove(result.ToString().LastIndexOf('
'), 1);
        reader.Close();
        response.Close();

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

推荐答案

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

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

reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();

这篇关于想使用 C# (FtpWebResponse) 从 FTP 读取文件列表,但它返回 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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