你的位置:首页 > ASP.net教程

[ASP.net教程]URL请求工具


工作中有个需求,定期请求多个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       try14       {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.AppSettings27         .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     /// 请求指定的url36     /// </summary>37     /// <param name="url"></param>38     static void Request(object url)39     {40       try41       {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         try66         {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.NewLine71             + msg72             + Environment.NewLine);73         }74         catch (Exception ex)75         {76           Console.Error.WriteLine(ex.ToString());77         }78       }79     }80   }

View Code

App.config:

 1 <??> 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