FTP error 227 entering passive mode

22,556

Try this example

http://social.msdn.microsoft.com/Forums/en-US/0128e595-c8e2-4f5e-9426-fd93eb510cab/the-remote-server-returned-an-error-227-entering-passive-mode-67228534212130

If you set UsePassive to false, then you need to make sure that the port for the command channel is open (i.e., you need to define endpoints and access rules). Unless there is a good reason to not use passive, you are far better off using passive.

Hope it will help.

Share:
22,556
echbel
Author by

echbel

Updated on September 30, 2020

Comments

  • echbel
    echbel over 3 years

    i'm trying to upload file to ftp server. tried some code samples, but alway getting this error, entering passive mode. for example, i can create a directory with this code

    FtpWebRequest reqFTP;
    try
    {
        // dirName = name of the directory to create.
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(
                 new Uri("ftp://" + ftpServerIP + "/" + dirName));
        reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        reqFTP.UsePassive = false;
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        Stream ftpStream = response.GetResponseStream();
    
        ftpStream.Close();
        response.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    

    or for example i can rename a file. but cannot upload file with this code

    string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
    FtpWebRequest reqFTP;
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
             "ftp://" + ftpServerIP + "/" + fileInf.Name));
    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
    reqFTP.KeepAlive = false;
    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
    reqFTP.UseBinary = true;
    reqFTP.ContentLength = fileInf.Length;
    
    int buffLength = 2048;
    byte[] buff = new byte[buffLength];
    int contentLen;
    
    FileStream fs = fileInf.OpenRead();
    
    try
    {
        Stream strm = reqFTP.GetRequestStream();
        contentLen = fs.Read(buff, 0, buffLength);
        while (contentLen != 0)
        {
            strm.Write(buff, 0, contentLen);
            contentLen = fs.Read(buff, 0, buffLength);
        }
        strm.Close();
        fs.Close();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message, "Upload Error");
    }
    

    getting exception at reqFTP.GetRequestStream().

    If I use reqFTP.UsePassive=false then i get “

    The remote server returned an error: (500) Syntax error, command unrecognized”.

    What should i do?