我試圖使用FtpWebRequest獲取公司FTP上的文件大小。但是,每當我嘗試獲得響應時都會拋出異常。在下面的代碼中查看catch塊中的錯誤細節。550錯誤當我嘗試獲取FTP上的文件大小
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時,此代碼確實有效。響應的狀態描述表明操作是「不允許的」。難道我的辦公室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格式的響應,但我的工作是返回一個窗口格式的響應。也許這是無關的,但我想我會提到它。