将C#用于FTP文件到大型机,包括数据集 – 将FTP脚本转换为FtpWebRequest代码

我使用cmd(Windows)将文件发送到IBM Mainframe并且工作正常它是这样的:

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))。消息“远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限)。” 550无法存储远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)。

((FtpWebResponse)ex.Response).StatusDescription“550无法存储/’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 localpath remotepath 

它导致在FTP服务器上跟随调用:

 STOR remotefile 

同样,如果你使用FtpWebRequest和URL这样的

 ftp://example.com/remotepath 

是在FTP服务器上跟随(相同)调用的结果:

 STORE remotepath 

请注意,省略了hostname( 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#代码的会话记录:

 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脚本的会话记录:

 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命令应该是相同的。