我有一段代码来获取所有文件的列表,这是一个Ftp服务器,但是我随机收到此错误连接尝试失败,因为连接的一方在一段时间后未正确响应,或者建立的连接失败,因为连接的主机未能响应xxx.xx.xx.xx:xxxxx远程服务器返回错误:227进入被动模式
这是我正在使用的代码,但出现此错误:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(("..." + ftpFolderPath));
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(@"...", @"...");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
string names = string.Empty;
if (response.StatusCode == FtpStatusCode.EnteringPassive)
{
return null;
}
else if (response.StatusCode == FtpStatusCode.OpeningData)
{
try
{
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
names = reader.ReadToEnd();
reader.Close();
responseStream = null;
reader = null;
}
catch (Exception)
{
return null;
}
response.Close();
request = null;
response = null;
GC.Collect();
}
任何想法我该如何解决?