URL请求工具

2018-06-18 02:09:04来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

工作中有个需求,定期请求多个URL。“定期”采用计划任务实现,请求URL,虽说start url可以实现,但不灵活。自己制作了个专门请求URL的工具,并记录请求结果。

控制台程序代码:

 1 class Program
 2     {
 3         //日志目录
 4         static string logFileDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Log", DateTime.Now.ToString("yyyy-MM"));
 5         //日志文件
 6         static string logFileName = DateTime.Now.ToString("yyyyMMdd") + ".txt";
 7         //日志文件锁定对象
 8         static object logLockObject = new object();
 9 
10         static void Main(string[] args)
11         {
12             //创建日志目录
13             try
14             {
15                 if (!Directory.Exists(logFileDirectory))
16                 {
17                     Directory.CreateDirectory(logFileDirectory);
18                 }
19             }
20             catch (Exception ex)
21             {
22                 Console.Error.WriteLine(ex.ToString());
23             }
24 
25             //执行请求任务
26             var tasks = ConfigurationManager.AppSettings
27                 .AllKeys.Where(key => key.StartsWith(ConfigurationManager.AppSettings["urlSettingsKeyPrefix"]))
28                 .Select(key => ConfigurationManager.AppSettings[key])
29                 .Select(url => Task.Factory.StartNew(Request, url)).ToArray();
30 
31             Task.WaitAll(tasks);
32         }
33 
34         /// <summary>
35         /// 请求指定的url
36         /// </summary>
37         /// <param name="url"></param>
38         static void Request(object url)
39         {
40             try
41             {
42                 var request = WebRequest.Create(url.ToString()) as HttpWebRequest;
43                 request.Timeout = Timeout.Infinite;
44                 using (var response = request.GetResponse())
45                 using (var responseStream = response.GetResponseStream())
46                 using (var streamReader = new StreamReader(responseStream))
47                 {
48                     WriteLog(streamReader.ReadToEnd(), url);
49                 }
50             }
51             catch (Exception ex)
52             {
53                 WriteLog(ex.ToString(), url);
54             }
55         }
56 
57         /// <summary>
58         /// 写日志
59         /// </summary>
60         /// <param name="msg"></param>
61         private static void WriteLog(string msg, object requestUrl)
62         {
63             lock (logLockObject)
64             {
65                 try
66                 {
67                     File.AppendAllText(
68                         Path.Combine(logFileDirectory, logFileName),
69                         string.Format("[#{0}]{1} {2}", Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), requestUrl)
70                         + Environment.NewLine
71                         + msg
72                         + Environment.NewLine);
73                 }
74                 catch (Exception ex)
75                 {
76                     Console.Error.WriteLine(ex.ToString());
77                 }
78             }
79         }
80     }
View Code

App.config:

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3     <startup> 
 4         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
 5     </startup>
 6   <appSettings>
 7     <add key="urlSettingsKeyPrefix" value="urlprefix-"/>
 8     
 9     <add key="urlprefix-url1" value="http://xxxxxx"/>
10     <add key="urlprefix-url2" value="http://xxxxxx"/>
11     <add key="urlprefix-url3" value="http://xxxxxx"/>
12   </appSettings>
13 </configuration>
View Code

 实际使用时,还需要设置下面这个才能发挥多线程的优势:

ServicePointManager.DefaultConnectionLimit = 20; //可以设置大点

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Z.ExtensionMethods 一个强大的开源扩展库

下一篇:AutoMapper 使用实践