如何解决FtpWebRequest“基础连接已经关闭;接收时发生错误”

arlang 2007-11-09 04:08:59
本人环境:IIS 5.0做FTP服务器,其中FTP服务器,“消息”选项卡中我加了一些中文欢迎词。
以下代码怎么调试都提示“基础连接已经关闭;接收时发生错误”,我经过查询相关资料得到以下方法解决此问题。

方法1:去掉FTP服务器中的中文欢迎词或改成英文欢迎词就不会发生错误,这是微软的一个BUG。
办法2:将中匿登录时密码(红色代码)用一个标准的邮箱如:test@hotmail.com,即使FTP服务器有中文欢迎词也不发生错误。

代码
public static string[] GetFileList()
{
string ftpServerIP = "localhost";
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
reqFTP.UseBinary = true;
reqFTP.KeepAlive = true;
reqFTP.Credentials = new NetworkCredential("anonymous", "asdf");
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
// to remove the trailing '\n'
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
return result.ToString().Split('\n');
}
catch (WebException ex)
{
Console.WriteLine(((FtpWebResponse)ex.Response).StatusCode); // 错误代码
Console.WriteLine(((FtpWebResponse)ex.Response).StatusDescription); // 错误描述
Console.WriteLine(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
...全文
1321 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
蓝色木 2009-02-19
  • 打赏
  • 举报
回复
顶一下

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧