title: File Transfer Protocol and an AS400
body: <p>I'm currently receiving the following error: </p>
  
  <p><em>The remote server returned an error: (501) Syntax error in parameters or arguments.</em></p>
  
  <p>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:</p>
  
  <pre><code>ftp
  open 192.168.1.2
  cd /Images
  get S12345.jpeg
  </code></pre>
  
  <p>That works correctly, however as soon as I attempt to connect via this code:</p>
  
  <pre><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;
  }
  </code></pre>
  
  <p>Can someone explain to me why this isn't working?</p>
  
  <ul>
  <li>Credentials work in Command Prompt</li>
  <li>File physically on server</li>
  <li>Can download from Command Prompt</li>
  </ul>
  
  <p>According the MSDN:</p>
  
  <blockquote>
    <p>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,
    "<a href="ftp://contoso.com" rel="nofollow">ftp://contoso.com</a>") the FtpWebRequest class provides the ability to
    programmatically interact with FTP servers.</p>
    
    <p>The URI may be relative or absolute. If the URI is of the form
    "<a href="ftp://contoso.com/%2fpath" rel="nofollow">ftp://contoso.com/%2fpath</a>" (%2f is an escaped '/'), then the URI is
    absolute, and the current directory is /path. If, however, the URI is
    of the form "<a href="ftp://contoso.com/path" rel="nofollow">ftp://contoso.com/path</a>", 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.</p>
  </blockquote>
  
  <p>Which is how the AS400 is expecting the data to come through.</p>