Search code examples
c#ftpmainframeftpwebrequest

Why FtpWebRequest is adding the user login as part of the target dataset?


I recently converted a Windows cmd to FtpWebRequest using C# to upload file to an IBM mainframe dataset. Look here.

Running both I found they upload to different dataset when they should be exactly alike, FtpWebRequest is adding the user login info to the dataset.

The code on cmd uploads the file to: 'ABCD.AA.C58FC.ABC1FD.ZP3ABC' dataset and the C# code upload the file to: USERME.ABCD.AA.C58FC.ABC1FD.ZP3ABC. I need the C# code to upload to the dataset: ABCD.AA.C58FC.ABC1FD.ZP3ABC. I know that they are uploading to different dataset because after uploading the file using C# if I use a cmd to get the file like:

get 'ABCD.AA.C58FC.ABC1FD.ZP3ABC' examplefile`

does not give me anything but if I do it like this:

get 'USERME.ABCD.AA.C58FC.ABC1FD.ZP3ABC' examplefile`

I got the file. What I'm missing here?

Here is the Windows cmd:

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

Here C# using FtpWebRequest

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

    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
    ftp.Credentials = new NetworkCredential(user, password);

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

    ftp.Method = WebRequestMethods.Ftp.UploadFile;

    FileStream fs = File.OpenRead(source);
    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();
}

Here is the network.log from C#:

System.Net Information: 0 : [8736] FtpWebRequest#53578024::.ctor(ftp://abc.wyx.state.aa.bb/ABCD.AA.C58FC.ABC1FD.ZP3ABC)
System.Net Information: 0 : [8736] FtpWebRequest#53578024::GetRequestStream(Method=STOR.)
System.Net Information: 0 : [8736] Current OS installation type is 'Client'.
System.Net Information: 0 : [8736] RAS supported: True
System.Net Error: 0 : [8736] Can't retrieve proxy settings for Uri 'ftp://abc.wyx.state.aa.bb/ABCD.AA.C58FC.ABC1FD.ZP3ABC'. Error code: 12180.
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Created connection from 10.11.12.134:56789 to 123.456.78.90:12.
System.Net Information: 0 : [8736] Associating FtpWebRequest#53578024 with FtpControlStream#38366678
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Received response [220-FTP 17:02:28 on 2015-05-26.
220-                          Warning!
220 Connection will close if idle for more than 5 minutes.]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Sending command [USER USERME]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Received response [331 Send password please.]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Sending command [PASS ********]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Received response [230 USERME is logged on.  Working directory is "USERME.".]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Sending command [OPTS utf8 on]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Received response [501 command OPTS aborted -- no options supported for utf8]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Sending command [PWD]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Received response [257 "'USERME.'" is working directory.]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Sending command [TYPE A]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Received response [200 Representation type is Ascii NonPrint]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Sending command [PASV]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Received response [227 Entering Passive Mode (204,122,60,60,157,239)]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Sending command [STOR ABCD.AA.C58FC.ABC1FD.ZP3ABC]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Received response [125 Storing data set USERME.ABCD.AA.C58FC.ABC1FD.ZP3ABC]
System.Net Information: 0 : [8736] FtpControlStream#38366678 - Received response [250 Transfer completed successfully.]
System.Net Information: 0 : [8736] FtpWebRequest#53578024::(Releasing FTP connection#38366678.)

Solution

  • Your code is correct and should work.


    When I use your code, my log shows:

    FtpWebRequest#7746814::.ctor(ftp://abc.wyx.state.aa.bb/'ABCD.AA.C58FC.ABC1FD.ZP3ABC')
    

    Note the quotes. But they are missing in your log:

    FtpWebRequest#53578024::.ctor(ftp://abc.wyx.state.aa.bb/ABCD.AA.C58FC.ABC1FD.ZP3ABC)
    

    Are you really showing us your exact code and matching log file?


    The WebRequest.Create creates an Uri class our of the URI string and calls .ToString() on that to log it.

    And obviously the

    new Uri(@"ftp://abc.wyx.state.aa.bb/'ABCD.AA.C58FC.ABC1FD.ZP3ABC'").ToString()
    

    is:

    ftp://abc.wyx.state.aa.bb/'ABCD.AA.C58FC.ABC1FD.ZP3ABC'
    

    What does it return for you?


    Note all that is happening well before it connects to the server. So there's no space for server-specific behavior.


    Also, what version of .NET framework are you using? In case this behavior has changed.