【发布时间】:2015-08-25 14:01:12
【问题描述】:
我找到了一个 Windows 命令行 sn-p 来从一个有效的 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 开源示例,包括以下示例,但我都遇到了错误。
例如,执行以下代码时,我得到一个错误
远程服务器返回错误:(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;
}
}
我假设我的 URL 一定是错误的。连接后,如何处理工作批处理文件示例中的cd(更改目录)命令?
如果有任何建议,我将不胜感激。谢谢。
【问题讨论】:
-
查看问题的右侧,在侧边栏中的“相关”下方。有什么用吗?
-
我看了很多帖子,包括那些。还没有任何工作。谢谢...
-
这个? stackoverflow.com/q/30357969/1927206。右上角的搜索框,输入
[mainfame] and [c#] and [ftp]。在过去的几个月里,它以两到三种方式出现。确保您按最近的顺序查看。
标签: c# ftp mainframe ftpwebrequest