I'm currently receiving the following error:
The remote server returned an error: (501) Syntax error in parameters or arguments.
I've physically checked the server and the file does indeed exist, if I open a Command Prompt and type the following code it works:
ftp
open 192.168.1.2
cd /Images
get S12345.jpeg
That works correctly, however as soon as I attempt to connect via this code:
private bool DownloadPod(string server)
{
string[] allocate = server.Split('\\');
string ftp = @"ftp://192.168.1.2/Images/" + allocate.Last();
Uri uri = new Uri(ftp);
// The code path for uri: ftp://192.168.1.2/Images/S12345.jpeg
var request = WebRequest.Create(uri) as FtpWebRequest;
if(request != null)
{
request.Method = WebRequestMethods.Ftp.DownloadFile;
// Left credentials off for security.
request.Credentials = new NetworkCredential(@"", @"");
// The line that triggers the error (response)
using(FtpWebResponse response = request.GetResponse() as FtpWebResponse)
using(Stream stream = response.GetResponseStream())
using(StreamReader reader = new StreamReader(stream))
{
reader.ReadToEnd();
return true;
}
}
return false;
}
Can someone explain to me why this isn't working?
According the MSDN:
To obtain an instance of FtpWebRequest, use the Create method. You can also use the WebClient class to upload and download information from an FTP server. Using either of these approaches, when you specify a network resource that uses the FTP scheme (for example, "ftp://contoso.com") the FtpWebRequest class provides the ability to programmatically interact with FTP servers.
The URI may be relative or absolute. If the URI is of the form "ftp://contoso.com/%2fpath" (%2f is an escaped '/'), then the URI is absolute, and the current directory is /path. If, however, the URI is of the form "ftp://contoso.com/path", first the .NET Framework logs into the FTP server (using the user name and password set by the Credentials property), then the current directory is set to /path.
Which is how the AS400 is expecting the data to come through.
In certain instances the /
character isn't valid or accepted.
The AS400 may require a the change by utilizing /%2F
, this will properly switch the directories on the AS400. For instance:
ftp://192.168.1.2/%2FImages/%2FS12345.jpeg
By using the /%2F
it allows it to move between directories.
Additional details can be found here.