0

I can create a folder, I can rename the file, but I can not upload and download a file to ftp. The exception that is shown is System.Net.WebException: The remote server returned an error: (500) Syntax error, command not recognized.

Anyone have an idea why?

log file: System.Net Information: 0 : [6112] FtpControlStream#7746814 - Resposta recebida [500 PORT/EPRT (Active Mode/Extended Active Mode) is not supported. Use PASV/EPSV instead of this] System.Net Information: 0 : [6112] FtpWebRequest#30923613::(Liberando a conexão de FTP#7746814.) System.Net Error: 0 : [6112] Exceção em FtpWebRequest#30923613::GetRequestStream - O servidor remoto retornou um erro: (500) Erro de sintaxe, comando não reconhecido.

Code:

 /* Upload File */
public void upload(string remoteFile, string localFile)
{
    try
    {
        /* Create an FTP Request */
        ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + remoteFile);
        /* Log in to the FTP Server with the User Name and Password Provided */
        ftpRequest.Credentials = new NetworkCredential(user, pass);
        /* When in doubt, use these options */
        ftpRequest.UseBinary = true;
        ftpRequest.UsePassive = false;
        ftpRequest.KeepAlive = true;

        /* Specify the Type of FTP Request */
        ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
        /* Establish Return Communication with the FTP Server */
        Stream ftpStream = ftpRequest.GetRequestStream();
        /* Open a File Stream to Read the File for Upload */
        FileStream localFileStream = new FileStream(localFile, FileMode.Create);
        /* Buffer for the Downloaded Data */
        byte[] byteBuffer = new byte[bufferSize];
        int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
        /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
        try
        {
            while (bytesSent != 0)
            {
                ftpStream.Write(byteBuffer, 0, bytesSent);
                bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
            }
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        /* Resource Cleanup */
        localFileStream.Close();
        ftpStream.Close();
        ftpRequest = null;
    }
    catch (Exception ex) { Console.WriteLine(ex.ToString()); }
    return;
}
  • I.e. use `ftpRequest.UsePassive = true;`, unless you have a **very good** reason not to. – Martin Prikryl Jun 02 '17 at 05:24
  • if I use ftpRequest.UsePassive = true; The exception that is shown is: The remote server returned an error: 227 Entering Passive Mode (201,23,75,26,225,86) – Thiago Schulz Jun 02 '17 at 11:27
  • And did you try to google that error message? If you did and none of the proposed solutions helped, edit your question for the new code and error. And make sure you include a log file, at least: https://stackoverflow.com/q/9664650/850848 – Martin Prikryl Jun 02 '17 at 11:33
  • The error happens in the command: Stream ftpStream = ftpRequest.GetRequestStream(); – Thiago Schulz Jun 02 '17 at 11:33
  • Sorry, but this is the first time I put a question here. I already read several post and I did not find a solution to this problem. Every time you pass in the stream comma ftpStream = ftpRequest.GetRequestStream (); Throws an exception. I already tested it on another ftp and the same problem happens. I do not know what else to do. – Thiago Schulz Jun 02 '17 at 11:49
  • Show us the log file! + *"I did not find a solution to this problem"* - What does that mean? Do you mean that you have tried all proposed solutions and none helped? Or that you believe that none of the proposed solutions match your situation? (Why?) – Martin Prikryl Jun 02 '17 at 11:51
  • I tried some solutions and none solved.How can I send you the log file, is it too large to post in the comments? – Thiago Schulz Jun 02 '17 at 12:10
  • Edit your question - All information should go to the question, not to the comments. – Martin Prikryl Jun 02 '17 at 13:06
  • Martin Prikryl Tanks for help me – Thiago Schulz Jun 02 '17 at 15:24

1 Answers1

0

My problem was the kaspersky antivirus that was blocking the communication port.

  • Please do not post duplicate answers: https://stackoverflow.com/a/24261401/850848 - You should have googled the answer, before asking! – Martin Prikryl Jun 02 '17 at 20:30