I am trying to create a small console application that downloads files from a ftp server through Explicit FTP over TLS. I have create these applications before but i am getting an error with this one. I keep Getting this error:

The Remote Server returned an error: 150 Opening BINARY mode data connection fro "filename" <2000 bytes>.

I cant seem to figure out that to do, can anyone help me?

this is my code:

 public void DownloadFiles(string fileName)
        {
            uri.Scheme = "ftp";
            uri.Host = ftpUrl;
            uri.Port = 21;
            uri.UserName = username;
            uri.Password = password;
            uri.Path = "out";
            try
            {
                FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri.ToString() + "/" + fileName));
                reqFTP.EnableSsl = true;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(username, password);
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                FtpWebResponse response;
                response = (FtpWebResponse)reqFTP.GetResponse();
                Stream responseStream = response.GetResponseStream();
                FileStream writeStream = new FileStream(localFolder + fileName, FileMode.Create);

                long length = response.ContentLength;
                int bufferSize = 2048;
                Byte[] buffer = new Byte[bufferSize];
                int readCount = responseStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    writeStream.Write(buffer, 0, readCount);
                    readCount = responseStream.Read(buffer, 0, bufferSize);
                }
                AppendLogFile(response, "Downloading Files: ", fileName);
                writeStream.Close();
                responseStream.Close();
                response.Close();
                reqFTP.Abort();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in DownloadFileByFileName method!! " + ex.Message);
            }
        }

thanks!

有帮助吗?

解决方案

I could not find the solution for this so i when over to use Chillkat for this application. works fine. But needed to buy a year license.

this is my code:

public void Connect(string fileName)
{
    bool success;
    success = ftp.UnlockComponent("license");
    if (!success)
    {
        Console.WriteLine(ftp.LastErrorText);
        return;
    }

    ftp.IdleTimeoutMs = 10000;
    ftp.AuthTls = true;
    ftp.Ssl = false;
    ftp.Hostname = ftpUrl;
    ftp.Port = 21;
    ftp.Username = username;
    ftp.Password = password;
    ftp.KeepSessionLog = true;

    success = ftp.Connect();
    if (success != true)
    {
        Console.WriteLine(ftp.LastErrorText);
        return;
    }

    ftp.ClearControlChannel();
    bool sucess = ftp.GetFile("out/" + fileName, localFolder + fileName);
    if (!success)
    {
        Console.WriteLine(ftp.LastErrorText);
        AppendErrorLogFile("Error in downloading file", "Download file method", fileName);
        return;
    }
    else
    {
        AppendLogFile("Download Success", "Download File Method", fileName);
        ftp.DeleteRemoteFile("out/" + fileName);
    }
}

其他提示

long length = response.ContentLength;

Is at fault here I believe, if you use this to download a file that already exists, your request will close before you can get a response from the server and throw the 150 error.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top