c# - FtpWeb 请求错误 : 550 Size not allowed in ASCII mode

标签 c# ftp binary ascii ftpwebrequest

我正在尝试通过匿名 FTP 从远程 FTP 文件获取文件大小。

public static long GetSize(string ftpPath)
{
    try
    {
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
        request.Proxy = null;
        request.Credentials = new NetworkCredential("anonymous", "´");
        request.UseBinary = true;
        request.Method = WebRequestMethods.Ftp.GetFileSize;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        long size = response.ContentLength;
        response.Close();
        return size;
    }
    catch (WebException e)
    {
        string status = ((FtpWebResponse)e.Response).StatusDescription;
        MessageBox.Show(status);
        return 0;
    }
}

这当前返回错误“550 Size not allowed in ASCII mode”。我知道我必须使用二进制模式,但是将 UseBinary 设置为 true(见上文)并不能解决问题。

最佳答案

不幸的是,我认为你可能被卡住了。 WebRequestMethods.Ftp 类,根据 this post ,将不支持发送受支持命令以外的 FTP 命令——对于您的用例,您需要客户端在发送 SIZE 命令之前发送“TYPE I”(用于“图像”或二进制模式)。

或者,作为一种 hacky 解决方法,您可以尝试在发送 SIZE 命令之前下载一个文件——任何文件。 request.UseBinary = true 对于 that 请求,它应该会导致您的客户端将“TYPE I”命令发送到 FTP 服务器。 (如果下载请求失败也无关紧要;TYPE 命令仍然会被发送。)大多数 FTP 服务器在收到 TYPE 命令后,将假定该 TYPE 用于后续命令。然后,当您再次尝试 GetFileSize 请求时,FTP 服务器可能是二进制的,而不是 ASCII 模式,并且您的 SIZE 命令可能会成功。

关于c# - FtpWeb 请求错误 : 550 Size not allowed in ASCII mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27595278/

相关文章:

java - FTP 客户端 - 列表文件

windows - 我的 FTP 批处理脚本卡在 "200 PORT command successful"上,无法将文件上传到服务器

c++ - 我能否确定从文件中读取的 32 字节二进制数据等于 256 位?

c# - Visual Studio 2015 无法识别 svg、rect、g、circle 等

c# - 将 Winforms 中的标签调整到左侧

c# - 编译错误 - 类型具有 [Application] 属性和 [assembly :Application] attribute

python - 使用 FTP 后端的 Django 文件上传

python - django 如何处理二进制 post 数据?

php - PHP 的 hex2bin 函数的 ColdFusion 替代方案

c# - 检索 VARCHAR(MAX)