【问题标题】:Download multiple files over FTP using one connection in .NET Core在 .NET Core 中使用一个连接通过 FTP 下载多个文件
【发布时间】:2020-05-29 20:50:49
【问题描述】:

我正在尝试仅通过一个打开的连接将多个文件下载到内存中。

我从这个文档中知道:https://docs.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest.keepalive - FtpWebRequest 默认 KeepAlive 为 true。

如果不应该破坏与服务器的连接;否则为假。默认值为 true。

我已经创建了这段代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace ConsoleApp23
{
    class Program
    {

        static void Main(string[] args)
        {
            String[] files = FTPListTree("ftp://server.com", "user", "password");

            for (int i = 0; i <= files.Count() - 1; i++)
            {
                string path = files[i].ToString();
                var request = (FtpWebRequest)WebRequest.Create(path);
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.Credentials = new NetworkCredential("user", "password");
                request.Timeout = 100000;
                request.KeepAlive = true;
                request.ConnectionGroupName = "DownloadFilesGroup";
                request.ServicePoint.ConnectionLimit = 8;

                using (var response = (FtpWebResponse)request.GetResponse())
                {
                    using (var stream = response.GetResponseStream())
                    {
                        var ms = new MemoryStream();
                        stream.CopyTo(ms);
                        var a = ms.ToArray();
                        string ss = Encoding.UTF8.GetString(a);
                        Console.WriteLine(ss);
                    }
                }
            }
        }

        public static String[] FTPListTree(String ftpUri, String user, String pass)
        {

            try
            {
                List<String> files = new List<String>();
                Queue<String> folders = new Queue<String>();
                folders.Enqueue(ftpUri);

                while (folders.Count > 0)
                {
                    String fld = folders.Dequeue();
                    List<String> newFiles = new List<String>();

                    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(fld);
                    ftp.Timeout = 20000;
                    ftp.Credentials = new NetworkCredential(user, pass);
                    ftp.Method = WebRequestMethods.Ftp.ListDirectory;

                    using (StreamReader resp = new StreamReader(ftp.GetResponse().GetResponseStream()))
                    {
                        String line = resp.ReadLine();
                        while (line != null)
                        {
                            newFiles.Add(line.Trim());
                            line = resp.ReadLine();
                        }
                    }

                    ftp = (FtpWebRequest)FtpWebRequest.Create(fld);
                    ftp.Timeout = 20000;
                    ftp.Credentials = new NetworkCredential(user, pass);
                    ftp.Method = WebRequestMethods.Ftp.ListDirectory;

                    using (StreamReader resp = new StreamReader(ftp.GetResponse().GetResponseStream()))
                    {
                        String line = resp.ReadLine();
                        while (line != null)
                        {
                            if (!Path.HasExtension(line))
                            {
                                String dir = newFiles.First(x => line.EndsWith(x));
                                newFiles.Remove(dir);
                                folders.Enqueue(fld + dir + "/");
                            }
                            line = resp.ReadLine();
                        }
                    }
                    files.AddRange(from f in newFiles select fld + f);
                }
                return files.ToArray();
            }
            catch (Exception e)
            {
                throw;
            }
        }
    }
}

这是一种正确的处置方式吗?我似乎找不到任何关于如果处置response 或响应流将关闭控制连接,keepalive 将保持打开状态的文档。

有什么方法可以在运行时记录它,以便我可以看到 FTP 调用?我自己无权访问 FTP 服务器。

更新

我实际上已经用测试 FTP 和上面的代码对此进行了测试,即使它有 KeepAlive=true,它在每个请求上都使用新连接。我创建了一个带有 Fluent API 的 PoC:https://github.com/robinrodricks/FluentFTP/blob/master/FluentFTP.CSharpExamples/DownloadManyFiles.cs,它正确地完成了:连接一次,然后下载文件。我有一种感觉,FtpWebRequest 就是这种情况。我不明白KeepAlive 及其作用。也许它重用了 TCP 连接,这本身是一件好事,但它使用 TCP 连接来登录每个请求。

是否有人知道FtpWebRequest 是否可以实现这一点,而不是像 Fluent FTP API 那样登录每个新请求?我错过了什么吗?

可以在此处查看 FTP 日志的示例

FTP 是 FileZilla Server 0.9.60 beta。

【问题讨论】:

    标签: c# .net .net-core ftp ftpwebrequest


    【解决方案1】:

    似乎 .NET Core 中没有实现 FTP 连接重用: https://github.com/dotnet/runtime/issues/23256

    作为*WebRequest API is actually obsolete,它可能永远不会。


    使用 .NET 框架,它可以按预期工作:
    FTP multiple files in C# without reestablishing connection

    【讨论】:

    • 非常感谢。我不知道过时的 *WebRequest。
    猜你喜欢
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多