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

问题描述 投票:2回答:1

我使用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);
}
c# .net ftp mainframe ftpwebrequest
1个回答
1
投票

您未指定运行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#代码的会话记录:

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

© www.soinside.com 2019 - 2024. All rights reserved.