工作中有個需求,定期請求多個URL。“定期”採用計劃任務實現,請求URL,雖說start url可以實現,但不靈活。自己製作了個專門請求URL的工具,並記錄請求結果。 控制台程式代碼: 1 class Program 2 { 3 //日誌目錄 4 static string logFileDirec ...
工作中有個需求,定期請求多個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