远程服务器错误:150打开二进制模式数据连接

时间:2014-03-04 11:47:53

标签: c# .net ftp ftpwebrequest

我正在尝试创建一个小型控制台应用程序,通过TLS上的Explicit FTP从ftp服务器下载文件。我以前创建过这些应用程序,但是我遇到了这个错误。我一直在犯这个错误:

  

远程服务器返回错误:150打开“filename”< 2000 bytes>的

的BINARY模式数据连接。

我似乎无法弄清楚要做什么,有人可以帮助我吗?

这是我的代码:

 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);
            }
        }

谢谢!

2 个答案:

答案 0 :(得分:0)

我无法找到解决方案,所以当我使用Chillkat进行此应用程序时。工作良好。但需要购买一年的许可证。

这是我的代码:

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);
    }
}

答案 1 :(得分:0)

long length = response.ContentLength;

此处有错误我相信,如果您使用此文件下载已存在的文件,您的请求将在您从服务器获得响应并抛出150错误之前关闭。