FtpWebRequest Connecting to an AS/400

2020-04-21 06:06发布

问题:

I need to download some files via ftp from an old AS/400 server. My code looks more or less like:

FtpWebRequest _request = (FtpWebRequest)WebRequest.Create("ftp://ftpaddress/FOO.CSV");
_request.Credentials = new NetworkCredential(_ftpUsername, _ftpPassword);
_request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)_request.GetResponse();

However, an exception is being thrown with the message:

501 Character (/) not allowed in object name.

I'm guessing the AS400 uses a different path separator than / but I can't figure out how to phrase the uri in a way (1) FtpWebRequest accepts and (2) the AS400 understands.

Anyone else bumped into this?

回答1:

According to this page, fwd slash is the path separator character:

The forward slash is the separator character for paths sent to the FTP server.

A similar conversation over at Microsoft's forums (2005 era) indicates it's a bug in FtpWebRequest:

Currently FtpWebRequest does not support quote and I cannot think of a way you'll be able to overide the method without exposing our code Mariya Atanasova [NCL]MSFT, Moderator, Nov 2005

Try updating to the most recent versions or try a different library; the MS forum thread has several.



回答2:

I've had this message often in the past, and it meant that I forgot to change the name format.

There are two name formats possible when doing FTP with an AS400, and it can be changed with the FTP command NAMEFMT:

0 is for the library system files (library\filename.member)

1 is for the files in the IFS, where a CSV file would be

By default, it is set to 0.

Change it to 1 and it should work. However I'm not sure how it can be changed with a FtpWebRequest.



回答3:

To make life a little bit easier, the FTP server decides what NameFormat you want to use, based on your first command. If you start with "cd /home", then the FTP server does automatically set NAMEFMT to 1 for you.

Indeed, you can change this manually during your session with the remote FTP command NAMEFMT. Please, notice that you don't need the (old) iSeries way. You can address EVERY object on the iSeries with NAMEFMT 1. For example, "get /QSYS.LIB/MYLIBRARY.LIB/MYFILE.FILE/MYMEMBER.MBR" will do the trick for any iSeries database table. Even for multimember files!



回答4:

This is an aggregate answer from the ones previously provided, but I was able to get this working by using the following structure:

ftp://[HostName]/%2F/[directory]/[subdirectory]/[filename].csv

The '%2F' was required and serves as a separator between the host name and the path.