计划的Windows服务中的一致FTP超时
问题描述:
我在Windows服务中遇到ftp问题。我已经安排了一份工作来通过ftp发送文件。有一段时间我有超时(频率每周一次或可能一个月一次),并继续,直到我重新启动我的Windows服务。计划的Windows服务中的一致FTP超时
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();
}
}
任何想法?
thnx提前。
答
为什么你不能把这一切放在一个循环?然后,如果你有错误,循环会返回并再次尝试。
另外,您为什么要将KeepAlive
选项设置为false?
我玩了一小会儿,并把它放到课堂上,这样我可以更好地看到它,但没有对它做任何测试。我的班级在后台线程中进行FTP调用,如果您希望能够与之进行通信,那么您肯定会这样做。
我当然不能保证这会起作用,但至少有一些故障!
class FtpRequests {
private const int BUF_SIZE = 10240;
private const string PASSWORD = "password";
private const string USERNAME = "username";
private const string SERVER = "yourserver.com";
private string path;
public FtpRequests() {
Cancel = false;
path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
}
public bool Cancel { get; set; }
public bool Complete { get; set; }
public Thread Thread1 { get; set; }
public int Timeout { get; set; }
public int ReadWriteTimeout { get; set; }
public void StartFtpDownload(string download, string file) {
string objString = string.Format("{0};{1}", download, file);
Thread1 = new Thread(startFtpThread);
Thread1.Name = string.Format("{0} download", file);
Thread1.IsBackground = true;
Thread1.Start(objString);
}
private void startFtpThread(object obj) {
Complete = false;
string objString = obj.ToString();
string[] split = objString.Split(';');
string download = split[0];
string file = split[1];
do {
try {
string uri = String.Format("ftp://{0}/{1}/{2}", SERVER, download, file);
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp) {
Cancel = true;
return;
}
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(USERNAME, PASSWORD);
reqFTP.KeepAlive = true;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.EnableSsl = false;
reqFTP.Proxy = null;
reqFTP.UsePassive = true;
reqFTP.Timeout = Timeout;
reqFTP.ReadWriteTimeout = ReadWriteTimeout;
using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse()) {
using (Stream responseStream = response.GetResponseStream()) {
using (FileStream writeStream = new FileStream(path + file, FileMode.Create)) {
Byte[] buffer = new Byte[BUF_SIZE];
int bytesRead = responseStream.Read(buffer, 0, BUF_SIZE);
while (0 < bytesRead) {
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, BUF_SIZE);
}
}
responseStream.Close();
}
response.Close();
Complete = true;
}
} catch (WebException wEx) {
LogDatabase.WriteLog("Download File", wEx.ToString(), "Download File");
}
} while (!Cancel && !Complete);
}
}
+0
这是一个精心安排的工作,每10分钟运行一次整个方法。 ...我的东西是一样的逻辑... – Florjon 2012-04-06 15:45:58
这听起来像是一个服务器上的问题......也许它正在忙于服务其他请求,并已达到最大连接数......你有任何控制权的服务器上?您可以在抛出异常时检查连接的客户端数量吗? – Gabber 2012-04-06 14:41:12
我没有访问服务器,因为我不允许。但据我所知,这是专门用于这项工作的服务器。 – Florjon 2012-04-06 14:45:11
你有没有想过为什么?我有同样的问题。我也必须重新启动服务。 – user1932634 2014-06-04 12:46:30