有时FTP会失败
本文关键字:失败 FTP 有时 | 更新日期: 2023-09-27 18:19:23
我的项目必须上传和下载大量的文件从Unix服务器和使用ftp在我的windows操作。我的代码是这样的
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(reviewfilepath);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = false;
ftpRequest.Proxy = null;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpStream = ftpRequest.GetRequestStream();
FileStream localFileStream = new FileStream(reviewsourcewordpath, FileMode.OpenOrCreate);
byte[] byteBuffer = new byte[bufferSize];
int bytesSent1 = localFileStream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesSent1 != 0)
{
ftpStream.Write(byteBuffer, 0, bytesSent1);
bytesSent1 = localFileStream.Read(byteBuffer, 0, bufferSize);
}
string path = cls_appvars.Set_App_Path + cls_appvars.Set_Log_dir + "SystemLog.txt";
System.IO.File.AppendAllText(path, System.DateTime.Now + "***ftp_documents() in cls_accdet***" + jobid + "_review ----- File uploaded Sucessfully" + Environment.NewLine);
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpRequest = null;
问题是FTP失败,得到异常
System.Net.WebException: The operation has timed out.
at System.Net.FtpWebRequest.GetRequestStream()
我在客户端机器上禁用了防火墙,启用了ftp.exe并给了用户完全的权限,但仍然得到异常。
谢谢,
Suressh
只要增加FtpWebRequest
超时,例如
ftpRequest.Timeout = 30000 // in milliseconds
FTP有时会由于丢包、某些连接限制或其他技术原因而失败。如果您的应用程序依赖于该传输,则在从该调用返回之前编写一些重试逻辑。将请求放入循环中,成功时返回内容,第三次失败时重新抛出异常。
将此添加到项目的配置文件中
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="100" />
</connectionManagement>
</system.net>
</configuration>