首页 新闻 搜索 专区 学院

FtpWebRequest请求超时,求解!!!

0
悬赏园豆:20 [已关闭问题] 关闭于 2020-06-23 11:36

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;
    }
大神带带我吧的主页 大神带带我吧 | 初学一级 | 园豆:25
提问于:2020-06-19 17:44
< >
分享
所有回答(1)
0

递归多次创建的request对象且未及时关闭导致线程死掉了

大神带带我吧 | 园豆:25 (初学一级) | 2020-06-23 11:36

请问最后怎么解决的

支持(0) 反对(0) TK0714 | 园豆:202 (菜鸟二级) | 2021-04-23 15:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册