我正在尝试连接到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:我给wirehark一个ftp过滤器,我可能已经发现了一些我不知道去哪里的信息......
编辑:新信息......我在另一个论坛上发现了一些信息,这些信息有点亮,但仍然没有提供答案......看起来文件大小没什么区别。下载一个小文件但在循环中休眠线程以延长下载时间具有相同的效果。似乎任何长于x时间的操作(其他帖子估计为100s)将到达结束并立即超时。另外我注意到在调试器中单步执行总接收字节数等于文件大小所以它正好在尝试读取文件的最后一个文件时发现没有任何内容,除非它从未找到,因为服务器从不告诉它!结束编辑
ftp服务器假设响应226 - 关闭数据连接。请求的文件操作成功(例如,文件传输或文件中止)。
然而,当我使用wireshark时,我可以看到它没有在它前面的[TCP Retransmission]进入。还不确定这是什么......仍然谷歌搜索。但我确信它有相关性。
所以看起来有些东西是超时或提前关闭但是对于FTP和某些新用户都是新手。我不知道从哪里开始。
我尝试了很多不同的东西:
好像是服务器把我踢了,但它与filezilla一起使用,所以必须有一些方法来避免这种情况。
编辑20/04/2012:它与filezilla(或资源管理器)无法正常工作我尝试使用资源管理器从ftp下载并收到操作已超时的消息。 Filezilla下载文件然后超时,但它巧妙地再次尝试使用REST命令。我怀疑实施这可能是理想的解决方案,所以我想知道这一点。如果我成功,将发布作为答案。结束编辑
需要注意的一点是,在传递WebException之前,会阻止访问下载的文件。所以在WebClient的情况下,我不能影响超时,它将长时间以100%挂起。然后抛出异常。如果我将FtpRequest超时设置为1000并处理该异常,则该文件似乎已完全下载,并且我能够在没有任何损坏的情况下提取它。
如果有人有任何提示或指示,甚至更好的解决方案:)我将非常感激。所有输入都将被接受。
谢谢
P.S我试图提供尽可能多的信息,但如果你需要更多信息,请告诉我。
答案 0 :(得分:0)
尝试查看源应用程序配置是否限制下载的大小。