問題描述
我有一個煩人的問題,阻止我在 FTP 中獲取我需要的文件.此文件可能有不同的名稱,因此我需要先訪問該文件夾并列出其中的文件,然后直接向該文件發出請求.
我的問題是,例如,我可以在 Filezilla 中訪問此文件,并且也完美地發現了該文件夾,但是當使用 FtpWebResponse
實例獲取該文件夾時,出現錯誤 550
550 文件不可用(例如文件未找到,無法訪問)
這里是代碼:
FtpWebRequest wr = (FtpWebRequest)WebRequest.Create("ftp://ftp.dachser.com/data/edi/kunden/da46168958/out");wr.Method = WebRequestMethods.Ftp.ListDirectoryDe??tails;wr.Credentials = new NetworkCredential(登錄"、密碼");FtpWebResponse 響應 = (FtpWebResponse)wr.GetResponse();流響應流 = response.GetResponseStream();StreamReader reader = new StreamReader(reponseStream);字符串名稱 = reader.ReadToEnd();
<塊引用>
FtpWebResponse 響應 = (FtpWebResponse)wr.GetResponse();
是拋出錯誤的那一行
PS:生產、測試??和 FileZilla 在同一個域中,使用相同的互聯網連接(如果有幫助)
感謝您的關注和反饋
FileZilla 日志:
來自我的程序的日志,紅色圈出的錯誤與 FTP 錯誤無關
當 FtpWebRequest
解釋 URL 時,它不會將分隔主機名和路徑的斜杠視為路徑的一部分.然后將提取的路徑按原樣與 FTP CWD
命令一起使用.這意味著 FTP 服務器將相對于您的主目錄解析路徑.如果您的帳戶未經過 chroot(客戶端未將 home 視為根),則缺少前導斜杠會導致意外行為.
在您的情況下,您從 /remote/path
開始并使用 ftp://example.com/remote/path/
之類的 URL,它將嘗試更改到 remote/path
,所以最終到 /remote/path/remote/path
.這不是你想要的.
- 您必須使用主文件夾的相對路徑.在您的情況下,這意味著使用沒有任何路徑的 URL.
- 或者使用絕對路徑,需要在主機名后使用兩個斜杠:
ftp://example.com//remote/path/
.
另請注意,文件夾的 URL 應以斜杠結尾:為什么 FtpWebRequest 會為此現有目錄返回空流?
其他 550 問題見FtpWebRequest 返回錯誤 550 文件不可用
I have an annoying problem preventing me to get a file I need in an FTP. This file may have differents names so I need to access the folder first and list files inside to do a request directly to the file then.
My problem is that I can access this file in Filezilla for example, and perfectly discovers the folder as well, but when using an FtpWebResponse
instance to get the folder, I have an error 550
550 File unavailable (e.g. file not found, no access)
here is the code :
FtpWebRequest wr = (FtpWebRequest)WebRequest.Create("ftp://ftp.dachser.com/data/edi/kunden/da46168958/out");
wr.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
wr.Credentials = new NetworkCredential("login", "password");
FtpWebResponse response = (FtpWebResponse)wr.GetResponse();
Stream reponseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(reponseStream);
string names = reader.ReadToEnd();
FtpWebResponse response = (FtpWebResponse)wr.GetResponse();
is the line throwing the error
PS: Production, tests and FileZilla are on the same domain, using the same internet connection (if it helps)
Thanks for your attention and feedback
The FileZilla logs:
Logs from my program, error circled in red isn't related to FTP error
When FtpWebRequest
interprets the URL, it does not consider the slash that separates the hostname and the path as a part of the path. The extracted path is then used with FTP CWD
command as is. That means that the FTP server will resolve the path relatively to your home directory. If your account is not chrooted (the home is not seen as the root by the client), the lack of the leading slash leads to unexpected behaviour.
In your case, you start in /remote/path
and with URL like ftp://example.com/remote/path/
, it will try to change to remote/path
, so ultimately to /remote/path/remote/path
. That's not what you want.
- Either you must use a relative path to the home folder. What in your case means using an URL without any path.
- Or use an absolute path, for which you need to use two slashes after the hostname:
ftp://example.com//remote/path/
.
Also note that an URL to a folder should end with a slash: Why does FtpWebRequest return an empty stream for this existing directory?
For other 550 problems, see FtpWebRequest returns error 550 File unavailable
這篇關于FtpWebRequest 返回“550 文件不可用(例如,文件未找到,無法訪問)"對存在的目錄使用 ListDirectoryDe??tails 時的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!