想要使用C#(FtpWebResponse)从FTP读取文件列表,但它返回HTML
我使用下面的代码从 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
会不会是网络代理干扰?尝试使用以下方法绕过代理:
Could it be a web proxy interfering? Try to get around the proxy by using the following:
这是通过 HTTP 代理使用
FtpWebRequest
的结果。文件列表使用 HTML 标签打印得非常漂亮,这些标签具有指向列表中各个文件的超链接。
如果您无法绕过代理,在我们的例子中,可以从封闭的
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 anXmlDocument
, and pull the file list out through.SelectNodes("//A/text()")
我找到了解决方案:默认代理意外启用,
我现在必须使用配置文件专门禁用它:
事实上,这确实是一个 .NET 问题!
I found the solution: the default proxy was enabled unexpectedly
I now have to disable it specifically using a config file:
In fact, it is really a .NET issue!
FTP 需要
PassiveMode
来下载、上传...相反,请尝试使用:
FTP Requires
PassiveMode
to download, upload...Instead, try to use: