C# 尝试使用C从IBM大型机下载#
我发现了一个Windows命令行代码片段,用于从一个运行正常的IBMz/OS大型机下载一个文件,现在我想使用C#和.NET复制这段代码。以下是两个Windows命令行文件: BatchFtp.bat:C# 尝试使用C从IBM大型机下载#,c#,ftp,mainframe,ftpwebrequest,C#,Ftp,Mainframe,Ftpwebrequest,我发现了一个Windows命令行代码片段,用于从一个运行正常的IBMz/OS大型机下载一个文件,现在我想使用C#和.NET复制这段代码。以下是两个Windows命令行文件: BatchFtp.bat: rem 999.999.99.99是大型机IP地址 ftp-s:C:\scripts\script.txt 999.999.99.99 暂停 C:\scripts\script.txt: myid MyPwd 光盘 获取“GIO.END.GPROD.PROC(MYDSNAME)”“c:\scri
rem 999.999.99.99是大型机IP地址
ftp-s:C:\scripts\script.txt 999.999.99.99
暂停
C:\scripts\script.txt:
myid
MyPwd
光盘
获取“GIO.END.GPROD.PROC(MYDSNAME)”“c:\scripts\GIO.END.GPROD.PROC(MYDSNAME.txt”
退出
我尝试了几个C#FTP开放源码示例,包括以下示例,但都出现了错误
例如,当执行以下代码时,我得到一个错误
远程服务器返回错误:(425)无法打开数据连接
执行此行时:
ftpStream = ftpResponse.GetResponseStream();
ftpRequest.UsePassive = true;
以下是我执行的代码:
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模式。正如“无法打开数据连接”消息所示,它存在一些问题
通过删除以下行,尝试使用活动模式:
ftpStream = ftpResponse.GetResponseStream();
ftpRequest.UsePassive = true;
不过请注意,一般来说,被动模式的问题较少。看见但是,当您证明主动模式在您的情况下有效时,请坚持这一点
回答另一个问题:无法使用
FtpWebRequest
发出cd
命令。只需使用完整路径即可:
ftp://host/../remotefile
那里有。/
而不是cd..
有关针对IBM大型机FTP服务器使用FtpWebRequest
路径的一般信息,请参阅:查看问题右侧,侧栏“相关”下方。有什么有用的吗?我看过很多帖子,包括那些。还没有什么有效的办法。谢谢…这个。在右上角的搜索框中,放置
[mainfame]和[c#]以及[ftp]
。在过去的几个月里,它以两到三种方式出现。确保你看的是最近的顺序。我删除了引号,因为我看到了一篇帖子,所以我添加了引号。我将UsePassive更改为true,现在我得到了错误“其他信息:远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)。”这看起来像是在进步。但是我知道该文件存在并且我有访问权限,因为DOS版本可以工作,所以完整路径一定不正确。谢谢那ftp://host/../GIO.END.GPROD.PROC(MydName)
(/../
是放cd的.
)你就是我希望见到的人:-)@Martin,很好的建议(“.”),但我也犯了同样的错误。谢谢。我在文件名周围重新添加了单引号,效果很好谢谢你的帮助,否则我可能不会明白的。