使用 C# 到 FTP 文件到大型机,包括数据集 - 将

Use C# to FTP file to mainframe including dataset - Translate FTP script to FtpWebRequest code(使用 C# 到 FTP 文件到大型机,包括数据集 - 将 FTP 脚本翻译成 FtpWebRequest 代码)
本文介绍了使用 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 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)