.net - FtpWebResponse 无法为 Ubuntu 服务器上的特定目录名称的 WebRequestMethods.Ftp.MakeDirectory 方法创建目录

标签 .net ubuntu ftp ftpwebrequest ftpwebresponse

我正在尝试对我的 .NET 应用程序通过 FTP(s) 协议(protocol)上传文件的功能进行压力测试。该函数使用.NET 内置的FtpWebResponse类(如果用户的服务器不支持 SSH 连接。)我正在使用以下代码尝试创建 "test up1/archive: * OR | or $ or < and >."我的用户目录中的 Ubuntu 服务器上的目录:

//First call succeeds
string strPath = "ftp://webhost.com/%2F/UserDir/" +
    Uri.EscapeDataString("test up1") + "/";
createDirViaFTP(strPath);

//but then this call fails
strPath += Uri.EscapeDataString("archive: * OR | or $ or < and >.") + "/";
createDirViaFTP(strPath);


static bool createDirViaFTP(string strURI)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(strURI);
    request.EnableSsl = bUseSsl;   //can be either false or true
    request.Credentials =  = new NetworkCredential(strUsrName, secUsrPwd);
    request.UseBinary = true;
    request.Timeout = -1;

    request.Method = WebRequestMethods.Ftp.MakeDirectory;

    try
    {
        using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
        {
            if (response.StatusCode == FtpStatusCode.PathnameCreated)
            {
                //Created OK
                return true;
            }
        }
    }
    catch(Exception ex)
    {
        //Failed
        Console.WriteLine("EXCEPTION: Path=" + strURI + "\n" + ex.Message);
    }

    return false;
}

第二次调用我的createDirViaFTP当我尝试创建 "archive: * OR | or $ or < and >." 时抛出以下异常目录:
EXCEPTION: Path=ftp://webhost.com/%2F/UserDir/test%20up1/archive%3A%20*%20OR%20%7C%20or%20%24%20or%20%3C%20and%20%3E./
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

但为什么?我在这里做错了什么?第二个目录名中的所有这些符号都应该是合法的 Linux 文件名。我可以通过 SSH shell 创建相同的目录。

最佳答案

我无法在 *nix ProFTPD FTP 服务器上创建这样的文件夹,即使我可以在 shell 中的同一系统上创建这样的文件夹。

在 FTP 中,我得到

550 archive: * OR | or $ or < and >.: Invalid directory name



这可能就是 FtpWebRequest也得到了。它只是盲目地将任何 550 错误转换为“文件不可用”。

所以我认为这不是您的代码的问题,而是 FTP 服务器对文件/目录名称的限制。

值得注意的是,ProFTPD 无条件地禁止在目录名称中使用星号。

core_mkd function :

MODRET core_mkd(cmd_rec *cmd) {
  int res;
  char *decoded_path, *dir;

  CHECK_CMD_MIN_ARGS(cmd, 2);

  /* XXX Why is there a check to prevent the creation of any directory
   * name containing an asterisk?
   */
  if (strchr(cmd->arg, '*')) {
    pr_response_add_err(R_550, _("%s: Invalid directory name"), cmd->arg);

    pr_cmd_set_errno(cmd, EINVAL);
    errno = EINVAL;
    return PR_ERROR(cmd);
  }
  ...

事实上,如果我删除 *从目录名称,创建成功。所以我猜,您也使用 ProFTPD FTP 服务器进行测试。

关于.net - FtpWebResponse 无法为 Ubuntu 服务器上的特定目录名称的 WebRequestMethods.Ftp.MakeDirectory 方法创建目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46552961/

相关文章:

git - 在 Ubuntu 上设置本地 git 存储库

php - 每 30 分钟自动删除 FTP 文件夹中的所有文件

windows - 批处理-FTP删除文件夹早于

c# - Func<T, TResult> 用于无效 TResult?

python - 由于 gcc 错误,无法使用 pip 安装 sasl

.net - Visual Studio : Simultaneously develop WCF client and server

zend-framework - Ubuntu、zend framework 和 PHPUnit - 测试说明 : PHPUnit was not found in your include_path, 因此不会创建任何测试操作

windows - VBScript 错误 - 使用用户名和密码创建 FTP 快捷方式

c# - WPF ComboBox : Trouble converting back-and-forth of Selected Item (Works in all cases, 但就是这个。)

c# - XML 签名适用于随机 RSA key ,但不适用于证书中的 key