想用C#(FtpWebResponse)来读取从FTP文件列表,但它返回的HTML [英] Want to use C# (FtpWebResponse) to read file list from FTP, but it returns HTML
本文介绍了想用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('\n') >= 0)
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
return result.ToString();
}
catch (Exception ex)
{
}
return null;
}
推荐答案
这可能是一个Web代理的干扰?尝试使用下面绕过代理:
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屋!
查看全文