0

Here's my current code:

    public bool ProcessFile(string fileName, string fileNameAndPath)
    {
        string address = string.Format("ftp://{0}/{1}", _address, fileName);
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnValidateCertificate);
        ServicePointManager.Expect100Continue = true;
        FtpWebRequest request = (FtpWebRequest) WebRequest.Create(address);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.EnableSsl = true;
        request.UseBinary = false;
        request.UsePassive = false;
        request.Credentials = new NetworkCredential(_username, _password);
        if (!File.Exists(fileNameAndPath))
        {
            Log(string.Format("{0}ERROR - Locating file to migrate. Path - {1}", Environment.NewLine, fileNameAndPath));
            return false;
        }
        try
        {
            StreamReader fileIn = new StreamReader(fileNameAndPath);
            byte[] fileContents = Encoding.UTF8.GetBytes(fileIn.ReadToEnd());
            fileIn.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            Log(string.Format("{0}File - {1} uploaded. Response Status: {2}", Environment.NewLine, fileNameAndPath, response.StatusDescription));
            response.Close();
            return true;
        }
        catch (Exception ex)
        {
            Log(string.Format("{0}ERROR - Upload routine. File - {1}. Error Exception: {2}", Environment.NewLine, fileNameAndPath, Util.GetFullError(ex)));
            return false;
        }
    }

Assume the port(21) is supplied with the address (host:21) and the credentials are valid. I am getting an exception on the GetRequestStream as follows :

The remote server returned an error: (501) Syntax error in parameters or arguments.

I can't seem to figure out the problem. I have checked everything. Yes, I need to use active mode - not able to do passive. Yes, it uses SSL/TLS. This should work but it doesn't and I am missing something here.

Need some hints or guidance on what could be going wrong here.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34
Michael
  • 507
  • 5
  • 20

1 Answers1

0

After enabling stack tracing I determined the error to be a missing port. Simply put my IT guy messed up and failed to properly set up active move for our FTP. Because the port assigned is returned in the initial connection and was not working, the FTP server rejected it as invalid and the .NET library interpreted the server error message. There was nothing wrong with my code.

Michael
  • 507
  • 5
  • 20