为啥 FtpWebRequest 恰好在长传输结束时抛出 WebException?
Posted
技术标签:
【中文标题】为啥 FtpWebRequest 恰好在长传输结束时抛出 WebException?【英文标题】:Why does FtpWebRequest throw a WebException exactly at the end of a long transfer?为什么 FtpWebRequest 恰好在长传输结束时抛出 WebException? 【发布时间】:2012-04-18 14:17:07 【问题描述】:我正在尝试连接到 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(或资源管理器)无法正常工作 我尝试使用资源管理器从 ftp 下载并收到操作已超时的消息。 Filezilla 下载文件然后超时,但它巧妙地使用 REST 命令再次尝试。我怀疑实施这可能是理想的解决方案,所以我要弄清楚这一点。如果我成功,将作为答案发布。结束编辑
需要注意的一点是,在传递 WebException 之前,对下载文件的访问被阻止。因此,在我无法影响超时的 WebClient 的情况下,它只会长时间挂在 100% 上。然后抛出异常。如果我将 FtpRequest 超时设置为 1000 并处理异常,则文件似乎已完全下载,并且我能够将其提取而不会损坏。
如果有人有任何提示或指示,甚至更好的解决方案:) 我将非常感激。所有输入都将被接受。
谢谢
P.S 我试图提供尽可能多的信息,但如果您需要更多信息,请告诉我。
【问题讨论】:
相关:FTPWebRequest is broken。我也遇到了这个问题。如果我这样做netstat
,即使我立即处理响应并且keepalive
是false
,我也会看到数千个打开的连接。如果您查看 FTPWebRequest
的 .NET 参考源,它似乎旨在故意保持连接打开,希望这些端口能够被重复使用,但实际上它每次都使用不同的端口......
...我目前的理论是它每次都尝试不同的端口,直到服务器开始拒绝连接。我写了一个程序,发送大量文件来测试问题。它可能真的用完了端口号,netstat 显示的端口从 49157 到 65535。请注意,我一次只发送一个文件。
【参考方案1】:
尝试查看源应用程序配置是否限制了下载大小。
【讨论】:
如果您的意思是在 app.config 中? ATM 中的所有内容都是跟踪和一些设置。我阅读了有关 httpRuntime 设置的信息,但这是 FTP。有没有我可以更改的类似设置?以上是关于为啥 FtpWebRequest 恰好在长传输结束时抛出 WebException?的主要内容,如果未能解决你的问题,请参考以下文章