2

I use a cmd (Windows) to send file to an IBM Mainframe and works fine it's something like this:

Open abc.wyx.state.aa.bb
User
Pass
lcd c:\Transfer>
Put examplefile 'ABCD.AA.C58FC.ABC1FD.ZP3ABC'
close
bye

I need to convert this to C#. I have been trying using FtpWebRequest but no luck. I cannot figure out how include the dataset I guess. When I run the application I got the following error:

((System.Exception)(ex)).Message "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." 550 Unable to store The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

((FtpWebResponse)ex.Response).StatusDescription "550 Unable to store /'ABCD.AA.C58FC.ABC1FD.ZP3ABC/examplefile'\r\n"

Here is what I got in C#

string user = "user";
string pwd = "password";

string ftpfullpath = @"ftp://abc.wyx.state.aa.bb//'ABCD.AA.C58FC.ABC1FD.ZP3ABC'/examplefile'";

try
{
     FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
     ftp.Credentials = new NetworkCredential(user, pwd);

     ftp.KeepAlive = true;
     ftp.UseBinary = false;  //Use ascii.              

     ftp.Method = WebRequestMethods.Ftp.UploadFile;

     FileStream fs = File.OpenRead(inputfilepath);
     byte[] buffer = new byte[fs.Length];
     fs.Read(buffer, 0, buffer.Length);
     fs.Close();

     Stream ftpstream = ftp.GetRequestStream();
     ftpstream.Write(buffer, 0, buffer.Length);
     ftpstream.Close();
}
catch (WebException ex)
{
     String status = ((FtpWebResponse)ex.Response).StatusDescription;
     throw new Exception(status);
}
Albert Torres
  • 163
  • 2
  • 15
  • 1
    have you thought about looking into the `Process.Start` to run the batchfile or are you wanting to send it using strictly C# code..? have you googled how to connect and send a file via FTP using C#.. there are tons of examples online – MethodMan May 20 '15 at 18:48
  • Yes it has to be on C#, and yes I look the link but I could not found it helpful for me. I'm using the same credential as my batch file which have no problem uploading the file. – Albert Torres May 20 '15 at 21:24
  • I just update the error message and replace the x with dummy names. examplefiles is the real name I use on the cmd script and works. – Albert Torres May 21 '15 at 14:08
  • Updated!, I will be more careful next time not to missed any questions. – Albert Torres May 22 '15 at 14:53
  • Appreciate it. I'm going to clear away the comments now. You had my upvote this morning. – Bill Woodger May 22 '15 at 15:10

1 Answers1

1

You didn't specify what platform you run the ftp script at. I assume Windows.

When you use Windows ftp command put like:

put localpath remotepath

it results in following call on the FTP server:

STOR remotefile

Similarly if you use FtpWebRequest with an URL like

ftp://example.com/remotepath

is results in following (same) call on the FTP server:

STORE remotepath

Note that the first slash after hostname (example.com) is omitted.


This means that your ftp script commands:

Open abc.wyx.state.aa.bb
...
Put examplefile 'ABCD.AA.C5879.ABC123.123ABC'

translate to FtpWebRequest URL like:

string ftpfullpath = @"ftp://abc.wyx.state.aa.bb/'ABCD.AA.C5879.ABC123.123ABC'";

Both result in this call on the FTP server:

STOR 'ABCD.AA.C5879.ABC123.123ABC'

Contrary, your ftp code with

string ftpfullpath = @"ftp://abc.wyx.state.aa.bb//'ABCD.AA.C5879.ABC123.123ABC'/examplefile'";

results in:

STOR /'ABCD.AA.C5879.ABC123.123ABC'/examplefile'

It does not look correct for mainframe.


Transcript of a session by my C# code:

USER user
331 Password required for user
PASS password
230 Logged on
OPTS utf8 on
200 UTF8 mode enabled
PWD
257 "/" is current directory.
TYPE A
200 Type set to A
PASV
227 Entering Passive Mode (zzz,zzz,zzz,zzz,193,162)
STOR 'ABCD.AA.C5879.ABC123.123ABC'
150 Connection accepted
226 Transfer OK

Transcript of the session by my ftp script:

USER user
331 Password required for user
PASS password
230 Logged on
PORT zzz,zzz,zzz,zzz,193,186
200 Port command successful
STOR 'ABCD.AA.C5879.ABC123.123ABC'
150 Opening data channel for file transfer.
226 Transfer OK
QUIT
221 Goodbye

I've tested this against FileZilla FTP server, so obviously the FTP server responses will differ on the mainframe FTP. But the FTP commands from the client should be the same.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Maybe I missing something but where on your code is the reference to the name of the file I trying to upload 'examplefile' – Albert Torres May 21 '15 at 14:12
  • Indeed the Put examplefile 'ABCD.AA.C5879.ABC123.123ABC' on Windows command prompt works. how your code is an equivalent to my ftp code if your is not including the file name? or maybe it's in a different way that I just don't get it. how I can write c# code to upload the file examplefile to the dataset 'ABCD.AA.C5879.ABC123.123ABC'? – Albert Torres May 21 '15 at 14:43
  • The filename is in `inputfilepath` (value of what is not shown in your code, but I assume it refers to a local file named `examplefile` = equivalent of the first argument of your `Put` command.). – Martin Prikryl May 21 '15 at 14:48
  • 1
    IT WORKS!! I just removed the file name from the ftpfullpath leave it only on the inputfilepath. Thank you so much for taking the time to help me out. – Albert Torres May 21 '15 at 15:20