如何在C#app中解决FTP超时问题

时间:2009-04-24 18:55:25

标签: c# ftp

我正在使用以下C#代码从远程服务提供商FTP一个~40MB的CSV文件。大约50%的时间,下载挂起并最终超时。在我的应用程序日志中,我得到一行:

> Unable to read data from the transport
> connection: A connection attempt
> failed because the connected party did
> not properly respond after a period of
> time, or established connection failed
> because connected host has failed to
> respond.

当我使用像LeechFTP这样的图形客户端以交互方式下载文件时,下载几乎不会挂起,并在大约45秒内完成。我有一段时间了解出了什么问题。

有人可以建议我如何使用此代码来更深入地了解正在发生的事情,或者更好地下载此文件的方法?我应该增加缓冲区大小吗?多少钱?避免缓冲写入磁盘并尝试吞下内存中的整个文件?任何建议表示赞赏!

...

private void CreateDownloadFile()
    {
        _OutputFile = new FileStream(_SourceFile, FileMode.Create);
    }
public string FTPDownloadFile()
    {
        this.CreateDownloadFile();

        myReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(this.DownloadURI));
        myReq.Method = WebRequestMethods.Ftp.DownloadFile;
        myReq.UseBinary = true;
        myReq.Credentials = new NetworkCredential(_ID, _Password);

        FtpWebResponse myResp = (FtpWebResponse)myReq.GetResponse();
        Stream ftpStream = myResp.GetResponseStream();

        int bufferSize = 2048;
        int readCount;
        byte[] buffer = new byte[bufferSize];
        int bytesRead = 0;
        readCount = ftpStream.Read(buffer, 0, bufferSize);

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

            Console.Write( '.' );    // show progress on the console
            bytesRead += readCount;
        }
        Console.WriteLine();
        logger.logActivity( "    FTP received " + String.Format( "{0:0,0}", bytesRead ) + " bytes" );

        ftpStream.Close();
        _OutputFile.Close();
        myResp.Close();
        return this.GetFTPStatus();
    }

    public string GetFTPStatus()
    {
        return ((FtpWebResponse)myReq.GetResponse()).StatusDescription;
    }

3 个答案:

答案 0 :(得分:2)

我尝试使用上面建议的FTPClient并得到相同的超时错误,FTPClient使用FtpWebRequest,所以我必须遗漏一些东西,但我不明白这一点。

经过一些研究后,我发现-1是无穷大的值

为了我的目的,可以使用无穷大,所以我就这样解决了问题。

这是我的代码:

//从FTP站点获取文件。

        FtpWebRequest reqFTP;

        string fileName = @"c:\downloadDir\localFileName.txt";
        FileInfo downloadFile = new FileInfo(fileName);
        string uri = "ftp://ftp.myftpsite.com/ftpDir/remoteFileName.txt";


        FileStream outputStream = new FileStream(fileName, FileMode.Append);

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.UseBinary = true;
        reqFTP.KeepAlive = false;
        reqFTP.Timeout = -1;
        reqFTP.UsePassive = 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);
        Console.WriteLine("Connected: Downloading File");
        while (readCount > 0)
        {
            outputStream.Write(buffer, 0, readCount);
            readCount = ftpStream.Read(buffer, 0, bufferSize);
            Console.WriteLine(readCount.ToString());
        }

        ftpStream.Close();
        outputStream.Close();
        response.Close();
        Console.WriteLine("Downloading Complete");

答案 1 :(得分:1)

我建议您不要使用FtpWebRequest进行FTP访问。 FtpWebRequest是我见过的最脑死亡的FTP API。

目前我使用的是FtpClient http://www.codeplex.com/ftpclient

我对IndySockets http://www.indyproject.org/index.en.aspx

也有好运

答案 2 :(得分:0)

我没有在代码级别处理FTP,但FTP支持恢复。也许你可以让它在超时时自动尝试恢复上传