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.
 
 
 
 
 
 

90 rivejä
3.5 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. System.GC.Collect();
  52. var url = System.Configuration.ConfigurationManager.AppSettings["cyapi"].ToString();
  53. var arr = url.Split(';');
  54. foreach (var item in arr)
  55. {
  56. try
  57. {
  58. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  59. myRequest.KeepAlive = false;
  60. myRequest.Method = "HEAD"; //设置提交方式可以为"get","head"等
  61. myRequest.Timeout = 30000; //设置网页响应时间长度
  62. myRequest.AllowAutoRedirect = false; //是否允许自动重定向
  63. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  64. // return (myResponse.StatusCode == HttpStatusCode.OK);//返回响应的状态
  65. System.IO.File.AppendAllText(logPath_timer,
  66. "\r\n" + DateTime.Now + "\r\n访问结果:" + myResponse.StatusCode);
  67. }
  68. catch (Exception ex)
  69. {
  70. System.IO.File.AppendAllText(logPath_timer,
  71. "\r\n" + DateTime.Now + "\r\n访问结果异常,异常信息:" + ex.Message + ",访问地址:" + item);
  72. }
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. System.IO.File.AppendAllText(logPath_timer,
  78. "\r\n" + DateTime.Now + "\r\n访问结果异常,异常信息:" + ex.Message);
  79. }
  80. }
  81. }
  82. }