我正在尝试使用c#中的ftp列出目录中的文件,我有以下代码:
StringBuilder result = new StringBuilder();
var reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(host + "/"));
reqFTP.Credentials = new NetworkCredential(user, pass);
reqFTP.Proxy = new WebProxy(proxy, proxyport)
{
Credentials = new NetworkCredential(proxyuser, proxypass, proxyuserdomain)
};
reqFTP.KeepAlive = false;
reqFTP.UsePassive = false;
reqFTP.UseBinary = false;
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; //also tried ListDirectory
using (var response = reqFTP.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
}
}
这将返回如下所示的HTML响应:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
<HEAD>
<TITLE>FTP Listing of /foo/bar/ at my.ftpste.com</TITLE>
<BASE HREF="ftp://foo:bar@my.ftpste.com.com/foo/bar/">
</HEAD>
<BODY>
<H2>FTP Listing of /foo/bar/ at my.ftpste.com</H2>
<HR>
<A HREF="../">Parent Directory</A><BR>
<PRE>
Nov 01 2011 17:27 4356 <A HREF="File1.txt">File1.txt</A>
Oct 22 2010 00:00 275051 <A HREF="File2.txt">File2.txt</A>
Oct 25 2010 00:00 1875 <A HREF="File3.txt">File3.txt</A>
Oct 26 2010 00:00 2553 <A HREF="File4.txt">File4.txt</A>
</PRE>
<HR>
</BODY>
</HTML>
请注意,响应已完成,包括关闭html标记。
问题是此文件列表不完整。当我在Internet Explorer中打开相同的网址时,使用相同的代理,我看到几乎相同的HTML,但列出了更多文件(例如File5.txt,File6.txt)。当我使用像filezilla这样的普通FTP客户端时也是如此。
我无法理解为什么会这样,有什么想法?