-->

在预定的窗口服务一致FTP超时(Consistent FTP timeout in a schedu

2019-07-28 20:03发布

我有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();
            }
        }

有任何想法吗 ?

日Thnx提前。

Answer 1:

你为什么不能把这一切都在一个循环? 然后,如果你有一个错误,环刚返回并再次尝试。

另外,为什么有你的设置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);
  }

}


文章来源: Consistent FTP timeout in a scheduled windows service