我刚刚在我们的一个开发服务器上对此进行了测试,确实有一个由 .NET FtpWebRequest 发出的 CWD:
从 172.16.3.210 到 172.16.3.210:21 的新连接(显式 SSL)
主机名解析:devpc
发送欢迎信息。
220 Gene6 FTP Server v3.10.0 (Build 2) 就绪...
用户测试用户
testuser, 331 testuser 需要密码。
测试用户,通过 ****
testuser,以“testuser”身份登录。
testuser, 230 用户 testuser 已登录。
testuser, OPTS utf8 on
testuser, 501 请先CLNT。
测试用户,残疾人
testuser, 257 "/" 是当前目录。
测试用户,CWD /
testuser,更改目录 '/' -> 'D:\testfolder' -> 允许访问。
testuser, 250 CWD 命令成功。“/”是当前目录。
测试用户,类型 I
testuser, 200 类型设置为 I。
测试用户,端口 172,16,3,210,4,127
testuser, 200 端口命令成功。
测试用户,NLST
testuser, 150 打开目录列表的数据连接。
testuser, 226 传输正常。
testuser, 421 连接关闭,超时。
测试用户,断开连接。(00d00:05:01)
在创建 FtpWebRequest 对象时,甚至没有在 uri 中指定“/”。
如果您调试或浏览源代码,一个名为“FtpControlStream”的类就会发挥作用。查看调用堆栈:
System.dll!System.Net.FtpControlStream.BuildCommandsList(System.Net.WebRequest req) 第 555 行 C#
System.dll!System.Net.CommandStream.SubmitRequest(System.Net.WebRequest 请求 =
{System.Net.FtpWebRequest}, bool async = false, bool readInitalResponseOnConnect = true) 第 143 行 C#
System.dll!System.Net.FtpWebRequest.TimedSubmitRequestHelper(bool async) 第 1122 行 + 0x13 字节 C#
System.dll!System.Net.FtpWebRequest.SubmitRequest(bool async = false) 第 1042 行 + 0xc 字节 C#
System.dll!System.Net.FtpWebRequest.GetResponse() 第 649 行 C#
有一个名为 BuildCommandsList() 的方法被调用。BuildCommandsList() 构建要发送到 FTP 服务器的命令列表。此方法具有以下代码片段:
if (m_PreviousServerPath != newServerPath) {
if (!m_IsRootPath
&& m_LoginState == FtpLoginState.LoggedIn
&& m_LoginDirectory != null)
{
newServerPath = m_LoginDirectory+newServerPath;
}
m_NewServerPath = newServerPath;
commandList.Add(new PipelineEntry(FormatFtpCommand("CWD", newServerPath), PipelineEntryFlags.UserCommand));
}
在第一次连接到服务器时,m_PreviousServerPath 始终为 null,newServerPath 的值为“/”,并由名为 GetPathAndFileName() 的函数计算(在此代码块之前的几行调用)。GetPathAndFileName() 如果没有提供路径或者如果在 'ftp://....' uri 的末尾明确添加了“/”,则将 newServerPath 计算为“/”。
所以这当然最终会导致 CWD 命令被添加到命令管道中,因为 null != "/"。
简而言之,不幸的是,您无法覆盖此行为,因为它已在源代码中被烧毁。