FtpWebRequest Download File(FtpWebRequest 下载文件)
问题描述
以下代码旨在通过 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();
}
错误是:
远程服务器返回错误:(550) 文件不可用(例如,找不到文件,无法访问)
The remote server returned an error: (550) File unavailable (e.g., file not found, no access)
该文件确实存在于远程计算机上,我可以手动执行此 ftp(即我有权限).谁能告诉我为什么会出现这个错误?
The file definitely 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服务器(使用用户名和凭据设置的密码属性),然后是当前目录设置为/path.
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 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:FtpWebRequest 下载文件


基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01