gpt4 book ai didi

c# - Use C# to FTP file to mainframe including dataset - 将 FTP 脚本转换为 FtpWebRequest 代码

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:12 25 4
gpt4 key购买 nike

我使用 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)).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 Unable to store /'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 如下:

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# 代码的 session 记录:

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

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

关于c# - Use C# to FTP file to mainframe including dataset - 将 FTP 脚本转换为 FtpWebRequest 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30357969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com