繁体   English   中英

使用不同的变量多次运行ac#.net控制台应用程序

[英]running a c# .net Console Application multiple time with different variables

我正在尝试使任务自动化。 我必须重复任务14次,并希望我的应用程序为我完成此任务。 我的应用程序运行一次就没有问题。 谁能解释我如何做到这一点而不重复代码?

我的应用程序从URL抓取html,然后将其保存到计算机上的某个位置。 (HTML是电子邮件签名)。 我需要该应用程序才能对14个不同的人执行此操作。

例。

// person1

字符串urlAddress1 =“ http://www.url.com/person1 ”;

文件流1 = @“ C:\\ Users \\ ellio \\ Desktop \\ test \\ person1.htm

// person2

字符串urlAddress2 =“ http://www.url.com/person2 ”;

文件流2 = @“ C:\\ Users \\ ellio \\ Desktop \\ test \\ person2.htm

等等

using System;
using System.Net;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace GetSignatureHtml
{

class Program
{
    static void Main(string[] args)
    {
        string urlAddress = "http://www.url.com/person1";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        if (response.StatusCode == HttpStatusCode.OK)
        {
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = null;

            if (response.CharacterSet == null)
            {
                readStream = new StreamReader(receiveStream);
            }
            else
            {
                readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
            }

            string data = readStream.ReadToEnd();

            using (FileStream fs = new FileStream(@"C:\Users\ellio\Desktop\test\person1.htm", FileMode.Create))
            {
                using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                {
                    w.WriteLine(data);
                }
            }

            Console.Write(data);
            Console.ReadKey(true);

            response.Close();
            readStream.Close();
        }
    }    

}

}

我建议将参数传递给Main方法, Main方法是一个包含所有要提取的URL的文件。 然后,您可以将所有这些URL读取到列表或数组中(例如,使用File.ReadAllLines ,并从URL推断输出文件。)将当前代码的大部分移至只接受URL的方法中,因此代码看起来像这个:

class Program
{
    static void Main(string[] args)
    {
        if (args.Length != 1)
        {
            // Display some error message here
            return;
        }

        string[] urls = File.ReadAllLines(args[0]);
        foreach (var url in urls)
        {
            DownloadUrl(url);
        }
    }

    private static void DownloadUrl(string url)
    {
        // Put most of your current code in here.
        // You need to infer the name of the file to save - 
        // consider using new Uri(url), then Uri.LocalPath
    }
}

然后,您只需要将URL放入文本文件中,并在运行代码时指定即可。

请注意,这可能与所有每一个命令行参数只是一个URL来完成,而是由时间你有一个命令行上14页的URL,这将是一个有点痛苦的检查。 我倾向于发现使用文件存储数据可以使生活更轻松。

作为代码其余部分的File.WriteAllText ,您应该使用using语句作为响应,并且我鼓励您使用File.WriteAllText作为创建文件的更简单方法。 尽管我不确定它是否以与您相同的方式应用响应编码,但您可能希望使用HttpClient作为整个方法的替代方法。

将您的工作移到一个方法中,然后遍历该方法。

using System;
using System.Net;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace GetSignatureHtml
{

    class Program
    {
        static void Main(string[] args)
        {
            foreach(arg in args)
            {
                FindData(arg.urlAddress);
            }
        }

        public void FindData(arg)  
        {
            string urlAddress = "http://www.url.com/person1";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream receiveStream = response.GetResponseStream();
                StreamReader readStream = null;

                if (response.CharacterSet == null)
                {
                    readStream = new StreamReader(receiveStream);
                }
                else
                {
                    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                }

                string data = readStream.ReadToEnd();

                using (FileStream fs = new FileStream(@"C:\Users\ellio\Desktop\test\person1.htm", FileMode.Create))
                {
                    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                    {
                        w.WriteLine(data);
                    }
                }

                Console.Write(data);
                Console.ReadKey(true);

                response.Close();
                readStream.Close();
            }
        }    
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM