gpt4 book ai didi

c# - 在不重新建立连接的情况下在 C# 中 FTP 多个文件

转载 作者:行者123 更新时间:2023-12-05 04:07:03 24 4
gpt4 key购买 nike

FTP 协议(protocol)旨在支持控制 channel ,并使用该控制 channel 告诉服务器打开 TCP 连接并传输文件。

发送或接收文件的服务器不必与 FTP 控制 channel 连接的服务器相同。可以是“三角”型连接。

它还允许客户端在控制 channel 上登录一次,并反复告诉服务器传输文件,而无需重新登录控制 channel 。

显然,当 MS 创建 C# FtpWebRequest 类时,这个概念已经完全脱离了 MS。

我需要做的正是 FTP 协议(protocol)的设计目的:

  1. 连接到服务器

  2. 传入凭据

  3. 创建目录(并愉快地忽略“已经存在”的错误)

  4. 反复向服务器传输文件

  5. 退出控制 channel

我肯定没有在 FtpWebRequest 类中看到这种能力。或者任何看起来允许 C# 代码中的这种流程的东西。

我看过了:

但是这一切似乎都无法按照预期的方式进行控制。

我可以指定 KeepAlive 属性,但循环必须重复调用 WebRequest.Create(targetName); 函数,这将创建一个新连接,并得到一个新的回应。然后它们就会超出范围或成为孤儿,因此根据定义,它们将被销毁。因此连接必须关闭,然后必须重新打开。对于数据连接,这没问题,但是操作 CONTROL 端口的能力在哪里?

如 FTP 规范所定义,该类不允许用户区分控制端口和数据端口。

有没有办法使用 C# 类按照预期的方式执行 FTP?因为在微软的狭隘思维中,整个世界看起来就像一个 HTTP Get/Response 协议(protocol)。

如有任何建议,我们将不胜感激。

-斯科蒂

最佳答案

在 .NET Framework 中,FtpWebRequest 在连接池之上工作。所以它实际上隐式重用一个底层的FTP连接,只要FtpWebRequest.KeepAlive设置为其默认值 true

KeepAlive 设置为 true 时,当请求完成时,底层 FTP(控制)连接不会关闭。当您使用相同的主机、端口和用户名创建 FtpWebRequest 的另一个实例时,来自先前请求的连接将被重用。


以两个文件上传请求到同一个 FTP 服务器的简单示例为例:

WebRequest request1 = WebRequest.Create("ftp://ftp.example.com/file1.zip");
request1.Credentials = new NetworkCredential("username", "password");
request1.Method = WebRequestMethods.Ftp.UploadFile;

using (Stream fileStream = File.OpenRead(@"C:\path\file1.zip"))
using (Stream ftpStream = request1.GetRequestStream())
{
fileStream.CopyTo(ftpStream);
}

WebRequest request2 = WebRequest.Create("ftp://ftp.example.com/file2.zip");
request2.Credentials = new NetworkCredential("username", "password");
request2.Method = WebRequestMethods.Ftp.UploadFile;

using (Stream fileStream = File.OpenRead(@"C:\path\file2.zip"))
using (Stream ftpStream = request2.GetRequestStream())
{
fileStream.CopyTo(ftpStream);
}

如果你enable .NET network logging ,你会看到只有一个FTP控制连接打开到服务器:

FtpWebRequest#45004109::.ctor(ftp://ftp.example.com/file1.zip)
FtpWebRequest#45004109::GetRequestStream(Method=STOR.)
Current OS installation type is 'Client'.
RAS supported: True
FtpControlStream#21454193 - Created connection from 127.0.0.1:60360 to 93.184.216.34:2121.
Associating FtpWebRequest#45004109 with FtpControlStream#21454193
FtpControlStream#21454193 - Received response [220 ...]
FtpControlStream#21454193 - Sending command [USER username]
FtpControlStream#21454193 - Received response [331 Password required for username]
FtpControlStream#21454193 - Sending command [PASS ********]
FtpControlStream#21454193 - Received response [230 Logged on]
FtpControlStream#21454193 - Sending command [OPTS utf8 on]
FtpControlStream#21454193 - Received response [202 UTF8 mode is always enabled. No need to send this command.]
FtpControlStream#21454193 - Sending command [PWD]
FtpControlStream#21454193 - Received response [257 "/" is current directory.]
FtpControlStream#21454193 - Sending command [TYPE I]
FtpControlStream#21454193 - Received response [200 Type set to I]
FtpControlStream#21454193 - Sending command [PASV]
FtpControlStream#21454193 - Received response [227 Entering Passive Mode (93,184,216,34,247,106)]
FtpControlStream#21454193 - Sending command [STOR file1.zip]
FtpControlStream#21454193 - Received response [150 Opening data channel for file upload to server of "/file1.zip"]
FtpControlStream#21454193 - Received response [226 Successfully transferred "/file1.zip"]
FtpWebRequest#45004109::(Releasing FTP connection#21454193.)
FtpWebRequest#58870012::.ctor(ftp://ftp.example.com/file2.zip)
FtpWebRequest#58870012::GetRequestStream(Method=STOR.)
Associating FtpWebRequest#58870012 with FtpControlStream#21454193
FtpControlStream#21454193 - Sending command [PASV]
FtpControlStream#21454193 - Received response [227 Entering Passive Mode (93,184,216,34,247,142)]
FtpControlStream#21454193 - Sending command [STOR file2.zip]
FtpControlStream#21454193 - Received response [150 Opening data channel for file upload to server of "/file2.zip"]
FtpControlStream#21454193 - Received response [226 Successfully transferred "/file2.zip"]
FtpWebRequest#58870012::(Releasing FTP connection#21454193.)

请注意,这在 .NET Core 中不起作用:
Download multiple files over FTP using one connection in .NET Core

关于c# - 在不重新建立连接的情况下在 C# 中 FTP 多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49012015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com