0

我创建了一个在 FTP 文件夹中查找文件的函数。

    public static List<string> GetFileList(NetworkCredential credential, string FTPSite, string FTPFolder, string extension)
    {
        string LSOutput = "";
        List<string> files = new List<string>();

            //Fetch LS
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://" + FTPSite + FTPFolder);
            request.Credentials = credential;
            request.UseBinary = true;
            request.EnableSsl = true;
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response = null;
            response = (FtpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            LSOutput = reader.ReadToEnd();
            reader.Close();
            response.Close();

            PrepareLog("LS output while finding files:");
            PrepareLog(LSOutput);


        //Parse the LS
        string[] LSOutputLines = LSOutput.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string LSOutputLine in LSOutputLines)
            files.Add(LSOutputLine);

        //Filter files
        files = files.Where(f => f.ToLower().EndsWith(extension.ToLower())).ToList();

        PrepareLogAndEmail("Total " + extension.ToLower() + " files found: " + files.Count, LogMessageType.Simple);
        return files;
    }

异常处理在函数被调用的地方完成。现在的问题是,有时 FTP 会遇到一些问题(如超时),然后我们必须重新运行程序。

我被要求添加 3 次重试机制,这就是我重试一次的方法。

    public static List<string> GetFileList(NetworkCredential credential, string FTPSite, string FTPFolder, string extension)
    {
        string LSOutput = "";
        List<string> files = new List<string>();

        try
        {
            //Fetch LS
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://" + FTPSite + FTPFolder);
            request.Credentials = credential;
            request.UseBinary = true;
            request.EnableSsl = true;
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response = null;
            response = (FtpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            LSOutput = reader.ReadToEnd();
            reader.Close();
            response.Close();

            PrepareLog("LS output while finding files:");
            PrepareLog(LSOutput);
        }
        catch (Exception ex)
        {
            PrepareLogAndEmail("First attempt failed: Details: " + ex.Message, LogMessageType.AttemptFailed);
            PrepareLogAndEmail("Second attempt started" + ex.Message, LogMessageType.Simple);

            //Fetch LS
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://" + FTPSite + FTPFolder);
            request.Credentials = credential;
            request.UseBinary = true;
            request.EnableSsl = true;
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response = null;
            response = (FtpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            LSOutput = reader.ReadToEnd();
            reader.Close();
            response.Close();

            PrepareLog("LS output while finding files(second attempt):");
            PrepareLog(LSOutput);
        }


        //Parse the LS
        string[] LSOutputLines = LSOutput.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string LSOutputLine in LSOutputLines)
            files.Add(LSOutputLine);

        //Filter files
        files = files.Where(f => f.ToLower().EndsWith(extension.ToLower())).ToList();

        PrepareLogAndEmail("Total " + extension.ToLower() + " files found: " + files.Count, LogMessageType.Simple);
        return files;
    }

现在,如果我必须再尝试 2 次,我是否需要进行这种日常链接,或者是否有其他更清洁的选项可用?

4

1 回答 1

2

我会使用重试循环,如下所示

您会注意到我使用布尔值来标记成功下载,或者重试计数终止。

我什至会更改最后一部分以检查下载是否成功,如果没有则通知用户,甚至可能记录错误消息。

    public static List<string> GetFileList(NetworkCredential credential, string FTPSite, string FTPFolder, string extension)
    {
        List<string> files = new List<string>();
        int retries = 0;
        int maxRetries = 5;
        bool downloaded = false;
        string LSOutput = "";
        while (!downloaded && retries < maxRetries)
        {
            try
            {
                LSOutput = "";
                //Fetch LS
                FtpWebRequest request = (FtpWebRequest) WebRequest.Create(@"ftp://" + FTPSite + FTPFolder);
                request.Credentials = credential;
                request.UseBinary = true;
                request.EnableSsl = true;
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                FtpWebResponse response = null;
                response = (FtpWebResponse) request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                LSOutput = reader.ReadToEnd();
                reader.Close();
                response.Close();

                PrepareLog("LS output while finding files:");
                PrepareLog(LSOutput);
                downloaded = true;
            }
            catch (Exception ex)
            {
                retries++;
            }
        }


        if (downloaded)
        {
            //Parse the LS
            string[] LSOutputLines = LSOutput.Trim().Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
            foreach (string LSOutputLine in LSOutputLines)
                files.Add(LSOutputLine);

            //Filter files
            files = files.Where(f => f.ToLower().EndsWith(extension.ToLower())).ToList();

            PrepareLogAndEmail("Total " + extension.ToLower() + " files found: " + files.Count, LogMessageType.Simple);
            return files;
        }
        else
        {
            PrepareLogAndEmail("Failed to download file", LogMessageType.Simple);
            return null;
        }
    }
于 2013-07-18T05:29:24.413 回答