【发布时间】:2011-02-16 10:07:23
【问题描述】:
以下代码旨在通过 FTP 检索文件。但是,我遇到了错误。
serverPath = "ftp://x.x.x.x/tmp/myfile.txt";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);
request.KeepAlive = true;
request.UsePassive = true;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
// Read the file from the server & write to destination
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) // Error here
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter destination = new StreamWriter(destinationFile))
{
destination.Write(reader.ReadToEnd());
destination.Flush();
}
错误是:
远程服务器返回错误:(550) 文件不可用(例如,找不到文件,无法访问)
该文件确实存在于远程机器上,我可以手动执行此 ftp(即我有权限)。谁能告诉我为什么会出现这个错误?
【问题讨论】:
-
我发现wireshark 对这样的东西很有用。您可以设置过滤器来查看您的机器和服务器之间的 FTP 流量。
-
如果将 UsePassive 设置为 false 会发生什么?我从来没有让任何服务器使用被动模式工作..
-
根据我的经验,这通常会导致超时错误,因为它尝试使用被防火墙阻止的端口。
-
嗯,据我所知,其余代码对我来说似乎没问题。
标签: c# .net ftp ftpwebrequest ftpwebresponse