我只是測試這對我們開發的服務器之一,確實有由.NET的FtpWebRequest發出了CWD:
new connection from 172.16.3.210 on 172.16.3.210:21 (Explicit SSL)
hostname resolved : devpc
sending welcome message.
220 Gene6 FTP Server v3.10.0 (Build 2) ready...
USER testuser
testuser, 331 Password required for testuser.
testuser, PASS ****
testuser, logged in as "testuser".
testuser, 230 User testuser logged in.
testuser, OPTS utf8 on
testuser, 501 Please CLNT first.
testuser, PWD
testuser, 257 "/" is current directory.
testuser, CWD/
testuser, change directory '/' -> 'D:\testfolder' --> Access allowed.
testuser, 250 CWD command successful. "/" is current directory.
testuser, TYPE I
testuser, 200 Type set to I.
testuser, PORT 172,16,3,210,4,127
testuser, 200 Port command successful.
testuser, NLST
testuser, 150 Opening data connection for directory list.
testuser, 226 Transfer ok.
testuser, 421 Connection closed, timed out.
testuser, disconnected. (00d00:05:01)
創建的FtpWebRequest對象時,這是沒有規定,甚至「/」中的URI 。
如果您調試或瀏覽源代碼,名爲'FtpControlStream'的類將發揮作用。請參閱調用堆棧:
System.dll!System.Net.FtpControlStream.BuildCommandsList(System.Net.WebRequest req) Line 555 C#
System.dll!System.Net.CommandStream.SubmitRequest(System.Net.WebRequest request =
{System.Net.FtpWebRequest}, bool async = false, bool readInitalResponseOnConnect = true) Line 143 C#
System.dll!System.Net.FtpWebRequest.TimedSubmitRequestHelper(bool async) Line 1122 + 0x13 bytes C#
System.dll!System.Net.FtpWebRequest.SubmitRequest(bool async = false) Line 1042 + 0xc bytes C#
System.dll!System.Net.FtpWebRequest.GetResponse() Line 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第一連接總是空,newServerPath的值是「/」和由名爲GetPathAndFileName函數來計算()(調用幾此代碼塊之前的行)。如果未提供路徑,或者在「ftp:// ....'uri」末尾明確指定了「/」,則GetPathAndFileName()會將newServerPath計算爲「/」。
所以這當然最終會導致CWD命令被添加到命令管道中,因爲null!=「/」。
簡而言之,不幸的是,您不能重寫此行爲,因爲它在源代碼中被燒燬。