550错误当我尝试获取FTP上的文件大小时

时间:2009-11-16 23:23:35

标签: c# ftpwebrequest

我正在尝试使用FtpWebRequest来获取公司FTP上的文件大小。然而,每当我尝试获得响应时,都会抛出异常。请参阅下面代码中catch块中的错误详细信息。

string uri = "ftp://ftp.domain.com/folder/folder/file.xxx";
FtpWebRequest sizeReq = (FtpWebRequest)WebRequest.Create(uri);
sizeReq.Method = WebRequestMethods.Ftp.GetFileSize;
sizeReq.Credentials = cred;
sizeReq.UsePassive = proj.ServerConfig.UsePassive; //true
sizeReq.UseBinary = proj.ServerConfig.UseBinary; //true
sizeReq.KeepAlive = proj.ServerConfig.KeepAlive; //false

long size;

try
{
//Exception thrown here when I try to get the response
using (FtpWebResponse fileSizeResponse = (FtpWebResponse)sizeReq.GetResponse())
{
size = fileSizeResponse.ContentLength;
}
}
catch(WebException exp)
{
FtpWebResponse resp = (FtpWebResponse)exp.Response;
MessageBox.Show(exp.Message); // "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
MessageBox.Show(exp.Status.ToString()); //ProtcolError
MessageBox.Show(resp.StatusCode.ToString()); // ActionNotTakenFileUnavailable
MessageBox.Show(resp.StatusDescription.ToString()); //"550 SIZE: Operation not permitted\r\n"
}

但是,当连接到我的个人FTP时,此代码可以正常工作。响应的StatusDescription表示操作“不允许”。可能是我的办公室FTP只是不允许查询文件大小?

我也尝试列出目录详细信息,这将返回大小,并注意到我的办公室FTP以不同的格式报告目录详细信息,然后是我的个人FTP。也许这就是问题?

//work ftp ListDirectoryDetails
-rw-r--r--   1 (?)      user    12345 Nov 16 20:28 some file name.xxx

//personal ftp ListDirectoryDetails
-rw-r--r--    1 user user 12345 Mar 13  some file name.xxx

从阅读this blog post我认为我的个人ftp正在返回一个Unix格式的响应,但我的工作是返回一个Windows格式的响应。也许这是无关的,但我想我会提到它。

3 个答案:

答案 0 :(得分:3)

由于文件的基本命名,我遇到了问题: 当您连接到FTP服务器时,您可以将Uri指定为“ftp // ftp.domain.com / somedirectory”,但这会转换为:“ftp://ftp.domain.com/homedirectoryforftp/somedirectory”。为了能够定义完整的根目录,请使用“ftp://ftp.domain.com//somedirectory”,它转换为机器上的某个目录。

答案 1 :(得分:1)

处理完登录后,我发现了一个默认用户目录作为当前目录。 我将完整路径指定为uri,包括我的用户目录。我用WireShark跟踪它,你可能会发现相同的结果。

答案 2 :(得分:0)

在工作FTP上看来,您没有指定所有者帐户,因此被拒绝。