我的项目必须在我的windows aoolication中使用ftp从Unix服务器上传和下载大量文件。 我的代码是这样的
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 failes几次失败并获得异常
System.Net.WebException: The operation has timed out.
at System.Net.FtpWebRequest.GetRequestStream()
我在客户端计算机中禁用了防火墙,启用了ftp.exe并为用户授予了完整权限,但仍然是异常。
谢谢,
Suressh
答案 0 :(得分:0)
只需增加FtpWebRequest
超时,例如
ftpRequest.Timeout = 30000 // in milliseconds
答案 1 :(得分:0)
FTP有时会因数据包丢失,某些连接限制或其他技术原因而失败,它会发生。如果您的App依赖于该传输,请在从该调用返回之前编写一些重试逻辑。将您的请求放入一个循环中,该循环在成功时返回内容,或者在第三个失败时重新抛出异常。
答案 2 :(得分:0)
将此添加到您的项目的配置文件中。
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="100" />
</connectionManagement>
</system.net>
</configuration>