使用 C# 将 FTP 文件传输到包括数据集在内的大型机 - 将 FTP 脚本转换为 FtpWebRequest 代码

Use C# to FTP file to mainframe including dataset - Translate FTP script to FtpWebRequest code

我使用 cmd (Windows) 将文件发送到 IBM 大型机并且工作正常,就像这样:

Open abc.wyx.state.aa.bb
User
Pass
lcd c:\Transfer>
Put examplefile 'ABCD.AA.C58FC.ABC1FD.ZP3ABC'
close
bye

我需要将其转换为 C#。我一直在尝试使用 FtpWebRequest 但没有运气。我不知道如何包含我猜的数据集。当我 运行 应用程序时,出现以下错误:

((System.Exception)(ex)).Message "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." 550 Unable to store The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

((FtpWebResponse)ex.Response).StatusDescription "550 Unable to store /'ABCD.AA.C58FC.ABC1FD.ZP3ABC/examplefile'\r\n"

这是我在 C# 中得到的

string user = "user";
string pwd = "password";

string ftpfullpath = @"ftp://abc.wyx.state.aa.bb//'ABCD.AA.C58FC.ABC1FD.ZP3ABC'/examplefile'";

try
{
     FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
     ftp.Credentials = new NetworkCredential(user, pwd);

     ftp.KeepAlive = true;
     ftp.UseBinary = false;  //Use ascii.              

     ftp.Method = WebRequestMethods.Ftp.UploadFile;

     FileStream fs = File.OpenRead(inputfilepath);
     byte[] buffer = new byte[fs.Length];
     fs.Read(buffer, 0, buffer.Length);
     fs.Close();

     Stream ftpstream = ftp.GetRequestStream();
     ftpstream.Write(buffer, 0, buffer.Length);
     ftpstream.Close();
}
catch (WebException ex)
{
     String status = ((FtpWebResponse)ex.Response).StatusDescription;
     throw new Exception(status);
}

您没有指定 运行 ftp 脚本所在的平台。我假设 Windows.

当你使用 Windows ftp 命令时 put 比如:

put localpath remotepath

它会在 FTP 服务器上产生以下调用:

STOR remotefile

类似地,如果您将 FtpWebRequest 与 URL 一起使用,例如

ftp://example.com/remotepath

是在 FTP 服务器上进行以下(相同)调用的结果:

STORE remotepath

请注意主机名 (example.com) 后的第一个斜杠被省略了。


这意味着您的 ftp 脚本命令:

Open abc.wyx.state.aa.bb
...
Put examplefile 'ABCD.AA.C5879.ABC123.123ABC'

翻译成FtpWebRequest URL喜欢:

string ftpfullpath = @"ftp://abc.wyx.state.aa.bb/'ABCD.AA.C5879.ABC123.123ABC'";

两者都导致在 FTP 服务器上进行此调用:

STOR 'ABCD.AA.C5879.ABC123.123ABC'

相反,您的 ftp 代码与

string ftpfullpath = @"ftp://abc.wyx.state.aa.bb//'ABCD.AA.C5879.ABC123.123ABC'/examplefile'";

结果:

STOR /'ABCD.AA.C5879.ABC123.123ABC'/examplefile'

大型机看起来不正确。


我的 C# 代码 session 的抄本:

USER user
331 Password required for user
PASS password
230 Logged on
OPTS utf8 on
200 UTF8 mode enabled
PWD
257 "/" is current directory.
TYPE A
200 Type set to A
PASV
227 Entering Passive Mode (zzz,zzz,zzz,zzz,193,162)
STOR 'ABCD.AA.C5879.ABC123.123ABC'
150 Connection accepted
226 Transfer OK

我的 ftp 剧本的 session 抄本:

USER user
331 Password required for user
PASS password
230 Logged on
PORT zzz,zzz,zzz,zzz,193,186
200 Port command successful
STOR 'ABCD.AA.C5879.ABC123.123ABC'
150 Opening data channel for file transfer.
226 Transfer OK
QUIT
221 Goodbye

我已经针对 FileZilla FTP 服务器进行了测试,因此显然 FTP 服务器响应在大型机 FTP 上会有所不同。但是来自客户端的FTP命令应该是一样的。