.net - 为什么 FtpWebRequest 恰好在长传输结束时抛出 WebException?

标签 .net ftp webclient ftpwebrequest system.net.sockets

我正在尝试连接到 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() 

2012 年 4 月 19 日编辑:我使用 ftp 过滤器试了一下 wireshark,我可能发现了一些我也不知道去哪里的更多信息...

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

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

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

所以看起来有些东西超时或提前关闭,但对 FTP 和 .net 来说都是新的,我不确定从哪里开始。

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

  • 保持事件=假
  • SetPointManager.MaxIdleTime
  • 增加服务器超时

似乎是服务器让我失望了,但它在 filezilla 上运行良好,所以必须有某种方法可以避免这种情况。

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

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

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

谢谢

P.S 我尝试提供尽可能多的信息,但如果您需要更多信息,请告诉我。

最佳答案

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

关于.net - 为什么 FtpWebRequest 恰好在长传输结束时抛出 WebException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10211307/

相关文章:

mysql - 周期性 MySql 超时,然后是 ASP.NET 网站中的连接峰值

java - Windows 下的 FileHandler Java

php - 就整体执行速度而言,哪个更快

c# - WebChannelFactory 和标题?

c++ - 使用 dllexport 时出错?

c# - 在后面的代码中更改光标

Azure Blob 存储作为源,FTP 作为目标

c# - 在 webclient 中获取通过 AJAX 生成的 html

c# - 异常处理 WebClient.DownloadString 的正确方法

.net - 为 SOAP 网络服务生成非双工代理代码