问题描述
我有一个烦人的问题,阻止我在 FTP 中获取我需要的文件.此文件可能有不同的名称,因此我需要先访问该文件夹并列出其中的文件,然后直接向该文件发出请求.
我的问题是,例如,我可以在 Filezilla 中访问此文件,并且也完美地发现了该文件夹,但是当使用 FtpWebResponse
实例获取该文件夹时,出现错误 550
550 文件不可用(例如文件未找到,无法访问)
这里是代码:
FtpWebRequest wr = (FtpWebRequest)WebRequest.Create("ftp://ftp.dachser.com/data/edi/kunden/da46168958/out");wr.Method = WebRequestMethods.Ftp.ListDirectoryDetails;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 文件不可用(例如,文件未找到,无法访问)"对存在的目录使用 ListDirectoryDetails 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!