我当前收到以下错误:
远程服务器返回错误:(501) 参数或参数中存在语法错误。
我已经物理检查了服务器,并且该文件确实存在,如果我打开命令提示符并键入以下代码,它就会起作用:
ftp
open 192.168.1.2
cd /Images
get S12345.jpeg
它工作正常,但是一旦我尝试通过此代码连接:
private bool DownloadPod(string server)
{
string[] allocate = server.Split('\\');
string ftp = @"ftp://192.168.1.2/Images/" + allocate.Last();
Uri uri = new Uri(ftp);
// The code path for uri: ftp://192.168.1.2/Images/S12345.jpeg
var request = WebRequest.Create(uri) as FtpWebRequest;
if(request != null)
{
request.Method = WebRequestMethods.Ftp.DownloadFile;
// Left credentials off for security.
request.Credentials = new NetworkCredential(@"", @"");
// The line that triggers the error (response)
using(FtpWebResponse response = request.GetResponse() as FtpWebResponse)
using(Stream stream = response.GetResponseStream())
using(StreamReader reader = new StreamReader(stream))
{
reader.ReadToEnd();
return true;
}
}
return false;
}
有人可以向我解释为什么这不起作用吗?
- 凭据在命令提示符下起作用
- 将文件物理存储在服务器上
- 可以从命令提示符下载
根据 MSDN:
To obtain an instance of FtpWebRequest, use the Create method. You can also use the WebClient class to upload and download information from an FTP server. Using either of these approaches, when you specify a network resource that uses the FTP scheme (for example, "ftp://contoso.com") the FtpWebRequest class provides the ability to programmatically interact with FTP servers.
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.
这就是 AS400 期望数据通过的方式。
最佳答案
在某些情况下,/
字符无效或不被接受。
AS400 可能需要使用 /%2F
进行更改,这将正确切换 AS400 上的目录。例如:
ftp://192.168.1.2/%2FImages/%2FS12345.jpeg
通过使用/%2F
,它允许它在目录之间移动。
更多详细信息请参见here .
关于c# - 文件传输协议(protocol)和 AS400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25570445/