试图使用c#从IBM大型机下载

本文关键字:IBM 大型机 下载 | 更新日期: 2023-09-27 18:09:48

我找到了一个Windows命令行代码片段,可以从IBM Z/OS大型机下载一个文件,现在我想使用c#和。net复制这个代码。下面是两个Windows命令行文件:

BatchFtp.bat:

rem 999.999.99.99 is mainframe IP Address
ftp -s:C:'scripts'script.txt 999.999.99.99
pause

C: ' ' script.txt脚本:

myid
MyPwd
cd ..
get "GIO.END.GPROD.PROC(MYDSNAME)" "c:'scripts'GIO.END.GPROD.PROC(MYDSNAME).txt"
quit

我已经尝试了几个c# FTP开源示例,包括以下,但我得到错误的所有。

例如,当执行以下代码时,我得到一个错误

远程服务器返回错误:(425)Can't open data connection.

当执行这一行时:

ftpStream = ftpResponse.GetResponseStream();

下面是我执行的代码:

private void button1_Click(object sender, EventArgs e)
{
    /* Download a File */
    Ftp ftpClient = new Ftp("999.999.99.99", "MyUserIdHere", "MypasswordHere");
    ftpClient.download(@"GIO.END.GPROD.PROC(DsNmHere)", @"'c:'junk'GIO.END.GPROD.PROC(DsNmHere).txt'");
}
class Ftp
{
    private string host = null;
    private string user = null;
    private string pass = null;
    private FtpWebRequest ftpRequest = null;
    private FtpWebResponse ftpResponse = null;
    private Stream ftpStream = null;
    private int bufferSize = 2048;
    /* Construct Object */
    public Ftp(string hostIP, string userName, string password)
    {
        host = hostIP; 
        user = userName; 
        pass = password;
    }
    /* Download File */
    public void download(string remoteFile, string localFile)
    {
        try
        {
            /* Create an FTP Request */
            string fullPath = @"ftp://" + this.host + "/'" + remoteFile + "'";
            ftpRequest = (FtpWebRequest) FtpWebRequest.Create(fullPath);
            /* Log in to the FTP Server with the User Name and Password Provided */
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            /* When in doubt, use these options */
            ftpRequest.UseBinary = false;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            /* Specify the Type of FTP Request */
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            /* Establish Return Communication with the FTP Server */
            ftpResponse = (FtpWebResponse) ftpRequest.GetResponse();
            /* Get the FTP Server's Response Stream */
            ftpStream = ftpResponse.GetResponseStream();
            /* Open a File Stream to Write the Downloaded File */
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            /* Buffer for the Downloaded Data */
            byte[] byteBuffer = new byte[bufferSize];
            int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
            /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
            try
            {
                while (bytesRead > 0)
                {
                    localFileStream.Write(byteBuffer, 0, bytesRead);
                    bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            /* Resource Cleanup */
            localFileStream.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
            Console.WriteLine(ex.ToString());
        }
        return;
    }
}    

我假设我的URL必须构造错误。连接后,如何处理在工作批处理文件示例中发现的cd(更改目录)命令?

我将感谢任何建议。谢谢。

试图使用c#从IBM大型机下载

您的ftp.exe脚本使用主FTP模式。(ftp.exe只支持主模式)

而你的c#代码使用被动FTP模式。它有一些问题,如"Can't open data connection"消息所示。

尝试使用活动模式,删除这一行:

ftpRequest.UsePassive = true;

请注意,一般情况下,被动语态的问题较少。请看我的指南来理解为什么被动语态现在很流行。但是,当您证明活动模式在您的情况下有效时,请坚持使用。


回答你的另一个问题:没有办法用FtpWebRequest发出cd命令。只需使用完整路径:

ftp://host/../remotefile

../代替了cd ..

有关在IBM大型机FTP服务器上使用FtpWebRequest路径的一般信息,请参见:
将FTP脚本转换为FtpWebRequest代码