LostCause
LostCause

Reputation: 151

FTP Upload and Download - getting either 227 or 500 error

I want to upload and download a file using FTP. I managed to put together the following code for my upload and download methods. I am stuck at the same place.

If I use:

ftpRequest.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponseStream();

It gives me 500 error. However, if I use just:

FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponseStream();

I get: The remote server returned an error: 227 Entering Passive Mode.

This is the same behaviour in both download and upload methods. I am able to upload the file using online clients so I know the server is set up fine. I disabled the firewall of my antivirus as some threads suggested but that doesn't work either. Now I have no idea what to do. My upload and download methods are as follows:

My Upload Method

private static void Upload ()
{
    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://ftp.myserver.com/");
    ftpRequest.Credentials = new NetworkCredential("username", "password");
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
    ftpRequest.UsePassive = false;
    FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
    StreamReader streamReader = new StreamReader(response.GetResponseStream());

    string line = streamReader.ReadLine();
    while (!string.IsNullOrEmpty(line))
    {
        Console.WriteLine(line);
        line = streamReader.ReadLine();
    }

    streamReader.Close();
}

My Download Method

FtpWebRequest reqFTP;
    try
        {
            FileStream outputStream = new FileStream(@"C:\download.csv", FileMode.Create);

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://ftp.myserver.com/upload/myfile.csv"));
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            reqFTP.UseBinary = true;
            reqFTP.UsePassive = false;
            reqFTP.KeepAlive = true;
            reqFTP.Credentials = new NetworkCredential("username", "password");
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream ftpStream = response.GetResponseStream();
            long cl = response.ContentLength;
            int bufferSize = 2048;
            int readCount;
            byte[] buffer = new byte[bufferSize];

            readCount = ftpStream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                outputStream.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);
            }

            ftpStream.Close();
            outputStream.Close();
            response.Close();
        }
        catch (Exception ex)
        {

        }

Upvotes: 0

Views: 586

Answers (1)

LostCause
LostCause

Reputation: 151

This is actually a perfectly working solution. Works on my buddy's laptop but not mine. Seems to be some antivirus settings.

Upvotes: 0

Related Questions