You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

92 lines
3.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.ServiceProcess;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace VisitService
  13. {
  14. public partial class QuanjiangDigitalSchollVisitService : ServiceBase
  15. {
  16. public QuanjiangDigitalSchollVisitService()
  17. {
  18. InitializeComponent();
  19. }
  20. private System.Timers.Timer timer1 = new System.Timers.Timer(60000);
  21. private static string pathbase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
  22. //private static string logPath = pathbase + @"\" + DateTime.Now.ToString("yyyyMMdd") + @"\log_start.txt";
  23. protected override void OnStart(string[] args)
  24. {
  25. timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
  26. timer1.AutoReset = true;
  27. timer1.Enabled = true;
  28. timer1.Start();
  29. }
  30. protected override void OnStop()
  31. {
  32. this.timer1.Enabled = false;
  33. }
  34. private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  35. {
  36. var logPath_timer = pathbase + @"\" + DateTime.Now.ToString("yyyyMMdd") + @"\log.txt";
  37. try
  38. {
  39. //记录日志
  40. if (!Directory.Exists(pathbase + @"\" + DateTime.Now.ToString("yyyyMMdd")))
  41. {
  42. //不存在文件夹则创建
  43. DirectoryInfo directoryInfo = new DirectoryInfo(pathbase + @"\" + DateTime.Now.ToString("yyyyMMdd"));
  44. directoryInfo.Create();
  45. }
  46. if (!File.Exists(logPath_timer))
  47. {
  48. FileStream fs = File.Create(logPath_timer);//创建文件
  49. fs.Close();
  50. }
  51. var url = System.Configuration.ConfigurationManager.AppSettings["cyapi"].ToString();
  52. var arr = url.Split(';');
  53. foreach (var item in arr)
  54. {
  55. try
  56. {
  57. System.IO.File.AppendAllText(logPath_timer,
  58. "\r\n" + DateTime.Now + "\r\n开始访问:" + item);
  59. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  60. myRequest.KeepAlive = false;
  61. myRequest.Method = "HEAD"; //设置提交方式可以为"get","head"等
  62. //myRequest.Timeout = 60000; //设置网页响应时间长度
  63. myRequest.AllowAutoRedirect = false; //是否允许自动重定向
  64. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  65. // return (myResponse.StatusCode == HttpStatusCode.OK);//返回响应的状态
  66. System.IO.File.AppendAllText(logPath_timer,
  67. "\r\n" + DateTime.Now + "\r\n访问结果:" + myResponse.StatusCode);
  68. myResponse.Dispose();
  69. }
  70. catch (Exception ex)
  71. {
  72. System.IO.File.AppendAllText(logPath_timer,
  73. "\r\n" + DateTime.Now + "\r\n访问结果异常,异常信息:" + ex.Message + ",访问地址:" + item);
  74. }
  75. }
  76. }
  77. catch (Exception ex)
  78. {
  79. System.IO.File.AppendAllText(logPath_timer,
  80. "\r\n" + DateTime.Now + "\r\n访问结果异常,异常信息:" + ex.Message);
  81. }
  82. }
  83. }
  84. }