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

时间:2015-05-20 18:46:01

标签: c# .net ftp mainframe ftpwebrequest

我使用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))。消息"远程服务器返回错误:(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);
}

1 个答案:

答案 0 :(得分:1)

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

使用Windows ftp命令put时:

put localpath remotepath

它导致在FTP服务器上跟随呼叫:

STOR remotefile

同样,如果您将FtpWebRequest

这样的网址一起使用
ftp://example.com/remotepath

导致在FTP服务器上进行以下(相同)调用:

STORE remotepath

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

这意味着你的ftp脚本命令

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

转换为FtpWebRequest网址,如:

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