FtpWebRequest returning “550 File unavailable (e.g. file not found, no access)” when using ListDirectoryDetails for a directory that exists

假装没事ソ 提交于 2020-11-29 03:21:33

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/64275643/ftpwebrequest-returning-550-file-unavailable-e-g-file-not-found-no-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!