FtpWebRequest错误:550 ASCII模式下不允许大小 [英] FtpWebRequest error: 550 Size not allowed in ASCII mode

查看:692
本文介绍了FtpWebRequest错误:550 ASCII模式下不允许大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  public static long GetSize(string ftpPath)
{
try
{
FtpWebRequest请求=(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();
返回大小;

catch(WebException e)
{
string status =((FtpWebResponse)e.Response).StatusDescription;
MessageBox.Show(status);
返回0;




$ b $ p
$ b

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

解决方案

不幸的是,我认为你可能会被卡住。 WebRequestMethods.Ftp 类,每个这篇文章,不支持发送除支持文件以外的FTP命令 - 对于您的用例,您需要客户端发送TYPE I(image或binary在发送SIZE命令之前,您可以尝试下载一个文件(任何文件)。或者,您也可以在发送SIZE命令之前尝试下载文件(任何文件)。使用 request.UseBinary = true 请求,它应该使您的客户端发送TYPE I命令到FTP服务器。 (下载请求失败并不重要; TYPE命令仍将被发送。)大多数FTP服务器在收到TYPE命令后,都会假定为后续命令的TYPE。然后,当您再次尝试 GetFileSize 请求时,FTP服务器可能是二进制文件,而不是ASCII模式,并且您的SIZE命令可能会成功。 / p>

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.

解决方案

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.

这篇关于FtpWebRequest错误:550 ASCII模式下不允许大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆