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 读取文件列表,但它
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
