使用 C# 到 FTP 文件到大型机,包括数据集 - 将 FTP 脚本翻译成 FtpWebRequest 代码 [英] Use C# to FTP file to mainframe including dataset - Translate FTP script to FtpWebRequest code

查看:30
本文介绍了使用 C# 到 FTP 文件到大型机,包括数据集 - 将 FTP 脚本翻译成 FtpWebRequest 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 cmd (Windows) 将文件发送到 IBM 大型机并且工作正常,如下所示:

I use a cmd (Windows) to send file to an IBM Mainframe and works fine it's something like this:

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

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

I need to convert this to C#. I have been trying using FtpWebRequest but no luck. I cannot figure out how include the dataset I guess. When I run the application I got the following error:

((System.Exception)(ex)).Message "远程服务器返回错误:(550) 文件不可用(例如,找不到文件,无法访问)."550 无法存储远程服务器返回错误:(550) 文件不可用(例如,找不到文件,无法访问).

((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 无法存储/'ABCD.AA.C58FC.ABC1FD.ZP3ABC/examplefile' "

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

这是我在 C# 中得到的内容

Here is what I got in 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.

You didn't specify what platform you run the ftp script at. I assume Windows.

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

When you use Windows ftp command put like:

put localpath remotepath

它会在 FTP 服务器上进行以下调用:

it results in following call on the FTP server:

STOR remotefile

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

Similarly if you use FtpWebRequest with an URL like

ftp://example.com/remotepath

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

is results in following (same) call on the FTP server:

STORE remotepath

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

Note that the first slash after hostname (example.com) is omitted.

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

This means that your ftp script commands:

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 服务器上调用:

Both result in this call on the FTP server:

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'

它看起来不适合大型机.

It does not look correct for mainframe.

我的 C# 代码的会话记录:

Transcript of a session by my C# code:

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

Transcript of the session by my ftp script:

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命令应该是一样的.

I've tested this against FileZilla FTP server, so obviously the FTP server responses will differ on the mainframe FTP. But the FTP commands from the client should be the same.

这篇关于使用 C# 到 FTP 文件到大型机,包括数据集 - 将 FTP 脚本翻译成 FtpWebRequest 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆