FtpWebRequest error: 550 Size not allowed in ASCII mode

笑着哭i 提交于 2019-12-08 00:25:36

问题


I'm trying to get the file size from a remote FTP file through anonymous 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;
    }
}

This currently returns the error "550 Size not allowed in ASCII mode." I'm aware that I have to use binary mode, but setting UseBinary to true (see above) doesn't fix the issue.


回答1:


Unfortunately, I think you may be stuck. The WebRequestMethods.Ftp class, per this post, will not support sending FTP commands other than the supported ones -- and for your use case, you would need your client to send "TYPE I" (for "image" or binary mode) before sending the SIZE command.

Alternatively, as a hacky workaround, you might try download a file -- any file -- before sending your SIZE command. With request.UseBinary = true for that request, it should cause your client to send the "TYPE I" command to the FTP server. (And it won't matter if that download request fails; the TYPE command will still have been sent.) Most FTP servers, upon receiving a TYPE command, will assume that TYPE for subsequent commands. Then, when you try the GetFileSize request again, the FTP server might be in binary, not ASCII mode, and your SIZE command might succeed.



来源:https://stackoverflow.com/questions/27595278/ftpwebrequest-error-550-size-not-allowed-in-ascii-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!