尝试使用 C# 从 IBM 大型机下载
Trying to download from IBM mainframe using C#
我找到了一个 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:\scripts\script.txt:
myid
MyPwd
cd ..
get "GIO.END.GPROD.PROC(MYDSNAME)" "c:\scripts\GIO.END.GPROD.PROC(MYDSNAME).txt"
quit
我尝试了几个 C# FTP 开源示例,包括以下示例,但我都遇到了错误。
比如我在执行下面的代码时,报错了
The remote server returned an error: (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
(更改目录)命令?
如果有任何建议,我将不胜感激。谢谢。
您的 ftp.exe
脚本使用活动 FTP 模式。 (ftp.exe
只支持主动模式。)
而您的 C# 代码使用被动 FTP 模式。正如 "Can't open data connection" 消息所示,它存在一些问题。
通过删除此行尝试使用活动模式:
ftpRequest.UsePassive = true;
但请注意,一般来说,被动模式往往问题较少。参见 my guide to understand why the passive mode prevails nowadays。但是当你证明主动模式适用于你的情况时,坚持下去。
回答您的另一个问题:无法使用 FtpWebRequest
发出 cd
命令。只需使用完整路径即可:
ftp://host/../remotefile
那里有 ../
而不是 cd ..
有关在 IBM 大型机 FTP 服务器上使用 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:\scripts\script.txt:
myid
MyPwd
cd ..
get "GIO.END.GPROD.PROC(MYDSNAME)" "c:\scripts\GIO.END.GPROD.PROC(MYDSNAME).txt"
quit
我尝试了几个 C# FTP 开源示例,包括以下示例,但我都遇到了错误。
比如我在执行下面的代码时,报错了
The remote server returned an error: (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
(更改目录)命令?
如果有任何建议,我将不胜感激。谢谢。
您的 ftp.exe
脚本使用活动 FTP 模式。 (ftp.exe
只支持主动模式。)
而您的 C# 代码使用被动 FTP 模式。正如 "Can't open data connection" 消息所示,它存在一些问题。
通过删除此行尝试使用活动模式:
ftpRequest.UsePassive = true;
但请注意,一般来说,被动模式往往问题较少。参见 my guide to understand why the passive mode prevails nowadays。但是当你证明主动模式适用于你的情况时,坚持下去。
回答您的另一个问题:无法使用 FtpWebRequest
发出 cd
命令。只需使用完整路径即可:
ftp://host/../remotefile
那里有 ../
而不是 cd ..
有关在 IBM 大型机 FTP 服务器上使用 FtpWebRequest
路径的一般信息,请参阅: