我在 Windows 服务中遇到 ftp 问题。我已经安排了一个通过 ftp 发送文件的作业。偶尔我会超时(频率每周一次或每月一次),它会一直持续到我重新启动我的 Windows 服务。
System.Net.WebException:操作已超时。
我正在处理异常,最后我关闭了所有打开的 ftp 会话。
try
{
string uri = String.Format("ftp://{0}/{1}/{2}", server, download, file);
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return;
}
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.EnableSsl = false;
reqFTP.Proxy = null;
reqFTP.UsePassive = true;
reqFTP.Timeout = Settings.Default.TimeOut;
reqFTP.ReadWriteTimeout = Settings.Default.TimeOut;
response = (FtpWebResponse)reqFTP.GetResponse();
responseStream = response.GetResponseStream();
using (FileStream writeStream = new FileStream(path + file, FileMode.Create))
{
int Length = 10240;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
}
response.Close();
}
catch (WebException wEx)
{
LogDatabase.WriteLog("Download File", wEx.ToString(), "Download File");
}
finally
{
if (response != null)
{
response.Close();
}
if (responseStream != null)
{
responseStream.Close();
}
}
有任何想法吗 ?
提前谢谢。