2

我正在尝试连接到 FTP 服务器并下载文件,但遇到了一些问题。

最初我使用WebClient.DownloadFileAsync,它适用于一个小文件,但一个大文件会到达下载的末尾,但从不调用DownloadFileCompleted ...

我尝试切换到 FtpWebRequest 并使用 FileStream 使用标准方法写入文件:

var FtpRequest = WebRequest.Create(uri) as FtpWebRequest
FtpRequest.Credentials = _Credentials;
FtpRequest.Method = WebRequestMethods.Ftp.DownloadFile;

using(var InputResponse = (FtpWebResponse)FtpRequest.GetResponse())
using (var InputStream = InputResponse.GetResponseStream())
using (var OutputStream = new FileStream(_DownloadDirectory + "\\" + fileName, FileMode.Create))
{
    var Buffer = new byte[1024];
    int TotalReadBytesCount = 0;
    int ReadBytesCount;
    while ((ReadBytesCount = InputStream.Read(Buffer, 0, Buffer.Length)) > 0)
    {
        OutputStream.Write(Buffer, 0, ReadBytesCount);
        TotalReadBytesCount += ReadBytesCount;
        var Progress = (int)(((double)TotalReadBytesCount / (double)FileSize) * 100);
        UpdateProgressBar(progressBar, Progress);
    }
}

这个也适用于小文件,但对于大文件,它将下载整个文件,然后我将在 InputStream.Read 处获得 System.Net.WebException。

编辑:异常移动取决于代码的结构...删除“使用”语句并关闭每个流,响应导致最后一个 x.close() 抛出异常。我还注意到 TotalBytesReceived 是 == 到 fileSize 所以下载在技术上是完整的。结束编辑

内部异常:底层连接已关闭:接收时发生意外错误。

启用 system.Net.Tracing 后,我得到以下日志文​​件:

System.Net Verbose: 0 : [2440] WebRequest::Create(ftp://ftp.******.com/)
System.Net Information: 0 : [2440] FtpWebRequest#63621045::.ctor(ftp://ftp.******.com/)
System.Net Verbose: 0 : [2440] Exiting WebRequest::Create()     -> FtpWebRequest#63621045
System.Net Verbose: 0 : [2440] FtpWebRequest#63621045::GetResponse()
System.Net Information: 0 : [2440] FtpWebRequest#63621045::GetResponse(Method=SIZE.)
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Created connection from ***.**.***.**:***** to **.**.**.**:**.
System.Net Information: 0 : [2440] Associating FtpWebRequest#63621045 with FtpControlStream#44374744
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [220 Microsoft FTP Service]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [USER ******]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [331 Password required for cashipftp.]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [PASS ********]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [230 User logged in.]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [OPTS utf8 on]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [200 OPTS UTF8 command successful - UTF8 encoding now ON.]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [PWD]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [257 "/" is current directory.]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [SIZE Superintendents/MSK/Stock Keeper_be.zip]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [213 96601015]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [QUIT]
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [221 Goodbye.]
System.Net Information: 0 : [2440] FtpWebRequest#63621045::(Releasing FTP connection#44374744.)
System.Net Verbose: 0 : [2440] Exiting FtpWebRequest#63621045::GetResponse() 
System.Net Verbose: 0 : [2440] FtpWebResponse#50706457::Close()
System.Net Verbose: 0 : [2440] Exiting FtpWebResponse#50706457::Close() 
System.Net Verbose: 0 : [2440] WebRequest::Create(ftp://ftp.******.com/)
System.Net Information: 0 : [2440] FtpWebRequest#89223::.ctor(ftp://ftp.*****.com/)
System.Net Verbose: 0 : [2440] Exiting WebRequest::Create()     -> FtpWebRequest#89223
System.Net Verbose: 0 : [2440] FtpWebRequest#89223::GetResponse()
System.Net Information: 0 : [2440] FtpWebRequest#89223::GetResponse(Method=RETR.)
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Created connection from ***.**.***.**:***** to **.**.**.**:**.
System.Net Information: 0 : [2440] Associating FtpWebRequest#89223 with FtpControlStream#4015056
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [220 Microsoft FTP Service]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [USER ******]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [331 Password required for cashipftp.]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [PASS ********]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [230 User logged in.]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [OPTS utf8 on]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [200 OPTS UTF8 command successful - UTF8 encoding now ON.]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [PWD]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [257 "/" is current directory.]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [TYPE I]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [200 Type set to I.]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [PASV]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [227 Entering Passive Mode (77,44,60,82,106,69).]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [RETR *****]
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [125 Data connection already open; Transfer starting.]
System.Net Verbose: 0 : [2440] Exiting FtpWebRequest#89223::GetResponse() 
System.Net Information: 0 : [1312] ServicePoint#42865679 - Closed as idle.
System.Net.Sockets Error: 0 : [2440] Socket#10316078::UpdateStatusAfterSocketError() - TimedOut
System.Net.Sockets Error: 0 : [2440] Exception in Socket#10316078::Receive - 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.
System.Net Information: 0 : [2440] FtpWebRequest#89223::(Releasing FTP connection#4015056.)
System.Net Verbose: 0 : [2440] FtpWebResponse#19201658::Close()
System.Net Verbose: 0 : [2440] Exiting FtpWebResponse#19201658::Close() 

编辑 19/04/2012:我用 ftp 过滤器尝试了 wireshark,我可能发现了更多信息,我也不知道该去哪里......

编辑:新信息...我在另一个论坛上找到了一些信息,这些信息更加清楚但仍然没有提供答案...看起来文件大小没有区别。下载一个小文件但在循环中休眠线程以延长下载时间具有相同的效果。似乎任何超过 x 时间的操作(其他帖子估计为 100 秒)都会到达终点并立即超时。我还注意到,在调试器单步执行时,接收到的总字节数等于文件大小,因此它与它尝试读取文件的最后一个文件的结果完全相同,只是发现没有剩余,除非它永远不会发现,因为服务器永远不会告诉它!结束编辑

假设 ftp 服务器响应 226 - 关闭数据连接。请求的文件操作成功(例如,文件传输或文件中止)。

但是,当我使用wireshark 时,我可以看到它前面没有出现[TCP Retransmission]。还不知道这是什么......仍在谷歌搜索。但我确信它具有相关性。

所以看起来有些东西正在超时或提前关闭,但对于 FTP 和对 .net 来说都是新手,我不知道从哪里开始。

我尝试了很多不同的东西:

  • 保活=假
  • SetPointManager.MaxIdleTime
  • 增加服务器超时

似乎是服务器把我踢走了,但它与 filezilla 配合得很好,所以必须有一些方法来避免这种情况。

编辑 20/04/2012:它不适用于 filezilla(或资源管理器)我尝试使用资源管理器从 ftp 下载并收到操作已超时的消息。Filezilla 下载文件然后超时,但它巧妙地使用 REST 命令再次尝试。我怀疑实施这可能是理想的解决方案,所以我要弄清楚这一点。如果我成功,将作为答案发布。结束编辑

需要注意的一点是,在传递 WebException 之前,对下载文件的访问被阻止。因此,在我无法影响超时的 WebClient 的情况下,它只会长时间挂在 100% 上。然后抛出异常。如果我将 FtpRequest 超时设置为 1000 并处理异常,则文件似乎已完全下载,并且我能够将其提取而不会损坏。

如果有人有任何提示或指示,甚至更好的解决方案:) 我将不胜感激。所有输入都将被接受。

谢谢

PS 我试图提供尽可能多的信息,但如果您需要更多信息,请告诉我。

4

1 回答 1

0

尝试查看源应用程序配置是否限制了下载大小。

于 2012-04-18T17:06:48.877 回答