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('
') >= 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:想使用 C# (FtpWebResponse) 从 FTP 读取文件列表,但它


- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01