尝试使用C#从IBM大型机下载

时间:2015-08-25 14:01:12

标签: c# ftp mainframe ftpwebrequest

我找到了一个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)无法打开数据连接。

执行此行时:

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;
    }
}    

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

我将不胜感激任何建议。感谢。

1 个答案:

答案 0 :(得分:2)

您的ftp.exe脚本使用活动FTP模式。 (ftp.exe仅支持活动模式。)

虽然您的C#代码使用被动FTP模式。并且它有一些问题,因为“无法打开数据连接”消息表明。

通过删除此行尝试使用活动模式:

ftpRequest.UsePassive = true;

虽然注意到一般情况下被动模式往往不那么成问题。见my guide to understand why the passive mode prevails nowadays。但是当你证明主动模式适用于你的情况时,坚持下去。

要回答您的其他问题:无法使用cd发出FtpWebRequest命令。只需使用完整路径:

ftp://host/../remotefile

../代替cd ..

有关使用FtpWebRequest针对IBM大型机FTP服务器的路径的一般信息,请参阅:
Use C# to FTP file to mainframe including dataset - Translate FTP script to FtpWebRequest code