c# - 文件传输协议(protocol)和 AS400

标签 c# ftp

我当前收到以下错误:

远程服务器返回错误:(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/

相关文章:

c# - Entity Framework 不加载相关对象

javascript - 媒体 uploader "uploading"同一图像的多个实例

linux - Proftpd 完全隐藏目录

c# - FTP 在 C# 中传输期间更改 PGP 文件

apache - 我正在考虑阻止访问除这些(SSH/HTTP)之外的我网站的每个部分。这是一个好主意吗?

docker - 在Docker应用程序中保存和处理文件

c# - 无法从 UserControl 上的字符串表示错误创建 System.Guid 类型的对象

c# - SC 删除 <服务> 不起作用

c# - 必须设置属性 "sonar.cs.fxcop.assembly"并且项目必须已经构建

c# - 你应该包装工作单元吗?