当前位置:  开发笔记 > 编程语言 > 正文

禁用CD的C#FTP

如何解决《禁用CD的C#FTP》经验,为你挑选了1个好方法。

我正在尝试使用以下代码:

  string url = String.Format(@"SOMEURL");
  string user = "SOMEUSER";
  string password = "SOMEPASSWORD";

  FtpWebRequest ftpclientRequest = (FtpWebRequest)WebRequest.Create(new Uri(url));
  ftpclientRequest.Method = WebRequestMethods.Ftp.ListDirectory;
  ftpclientRequest.UsePassive = true; 
  ftpclientRequest.Proxy = null;
  ftpclientRequest.Credentials = new NetworkCredential(user, password);
  FtpWebResponse response = ftpclientRequest.GetResponse() as FtpWebResponse;

这通常有效,但对于1个特定服务器,这会产生错误500:语法无法识别.在问题服务器上禁用了"更改目录"命令,并且站点管理员告诉我,.NET默认情况下会发出一个"更改目录"命令,其中包含所有FTP连接.真的吗?有没有办法禁用它?
编辑:当我从命令行登录时,我在正确的目录中:
ftp> pwd
257"/"是当前目录



1> Kev..:

我刚刚在我们的一个开发服务器上测试了这个,确实有一个由.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始终为null,newServerPath的值为"/",由名为GetPathAndFileName()的函数计算(在此代码块之前调用几行).如果没有提供路径,或者在'ftp:// ....'uri的末尾显式添加了"/",则GetPathAndFileName()将newServerPath计算为"/".

所以这当然最终导致CWD命令被添加到命令管道,因为null!="/".

简而言之,您不能覆盖此行为,因为它在源中被烧毁.

推荐阅读
围脖上的博博_771
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有