FtpWebRequest从服务器下载文件夹(文件夹底下有6级文件夹深度,里面只有一个子文件(10kb左右)就会会报超时。而从根目录下下载130MB的文件不会超时
//获取遍历的文件集合
List<FtpFileInfo> result = new List<FtpFileInfo>();
public List<FtpFileInfo> GetFileList(string relativePath = null, string localSavePath = null)
{
FtpWebRequest request;
try
{
string path = string.Format("ftp://{0}:{1}", this.IpAddr, this.Port).ToString();
if (!string.IsNullOrEmpty(relativePath))
{
path += relativePath;
}
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
request.UseBinary = true;
request.UsePassive = true;
request.Timeout = 300000;//传输 5分钟
request.KeepAlive = true;
//设置用户名和密码
request.Credentials = new NetworkCredential(this.UserName, this.Password);
//设置ftp的命令
//仅获取列表
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
//是否区分文件夹/文件读取文件;若不区分,文件夹及文件会一起读取
FtpFileInfo newf = new FtpFileInfo();
//仅读取文件夹
if (line.Contains("<DIR>"))
{
newf.type = "dir";
newf.fileName = line.Substring(line.LastIndexOf("<DIR>") + 5).Trim();
newf.lastPath = relativePath;
newf.relationPath = relativePath + newf.fileName + "/";
newf.localPath = localSavePath + "\\" + newf.fileName;
result.Add(newf);
GetFileList(relativePath + newf.fileName + "/", localSavePath + "\\" + newf.fileName);
//result.Add(line.Substring(line.LastIndexOf("<DIR>") + 5).Trim());
}
else
{
//读取文件夹下文件
if (!line.Contains("<DIR>"))
{
newf.type = "file";
newf.fileName = line.Substring(39).Trim();
newf.lastPath = relativePath;
newf.relationPath = relativePath + newf.fileName;
newf.localPath = localSavePath + "\\" + newf.fileName;
result.Add(newf);
}
}
//读取下一行
line = reader.ReadLine();
}
reader.Close();
response.Close();
return result;
}
catch (Exception ex)
{
Console.WriteLine("get the files/folders from the ftp:" + ex.Message);
}
return null;
}
递归多次创建的request对象且未及时关闭导致线程死掉了
请问最后怎么解决的