的FtpWebRequest下载文件 [英] FtpWebRequest Download File
问题描述
以下code是为了获取通过FTP的文件。不过,我得到一个错误吧。
The following code is intended to retrieve a file via FTP. However, I'm getting an error with it.
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();
}
该错误是:
The remote server returned an error: (550) File unavailable (e.g., file not found, no access)
该文件肯定不会在远程机器上存在,我可以手动执行此FTP(即我有权限)。谁能告诉我为什么我可能会得到这个错误?
The file definately does exist on the remote machine and I am able to perform this ftp manually (i.e. I have permissions). Can anyone tell me why I might be getting this error?
推荐答案
这一段距离的 FptWebRequest类引用可能是你的兴趣:
This paragraph from the FptWebRequest class reference might be of interest to you:
的URI可能是相对或绝对的。
如果URI是形式
ftp://contoso.com/%2fpath(%2F是
转义'/'),那么URI是
绝对的,当前目录是
/路径。然而,如果URI是的
形成ftp://contoso.com/path,第一
在.NET Framework登录到FTP
服务器(使用的用户名和
密码由证书设置
属性),则当前目录
设置为/路径
The URI may be relative or absolute. If the URI is of the form "ftp://contoso.com/%2fpath" (%2f is an escaped '/'), then the URI is absolute, and the current directory is /path. If, however, the URI is of the form "ftp://contoso.com/path", first the .NET Framework logs into the FTP server (using the user name and password set by the Credentials property), then the current directory is set to /path.
这篇关于的FtpWebRequest下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!