|
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.ServiceProcess;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace VisitService
- {
- public partial class QuanjiangDigitalSchollVisitService : ServiceBase
- {
- public QuanjiangDigitalSchollVisitService()
- {
- InitializeComponent();
- }
-
- private System.Timers.Timer timer1 = new System.Timers.Timer(60000);
- private static string pathbase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
- //private static string logPath = pathbase + @"\" + DateTime.Now.ToString("yyyyMMdd") + @"\log_start.txt";
- protected override void OnStart(string[] args)
- {
- timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
- timer1.AutoReset = true;
- timer1.Enabled = true;
- timer1.Start();
- }
-
- protected override void OnStop()
- {
- this.timer1.Enabled = false;
- }
-
- private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- var logPath_timer = pathbase + @"\" + DateTime.Now.ToString("yyyyMMdd") + @"\log.txt";
- try
- {
- //记录日志
- if (!Directory.Exists(pathbase + @"\" + DateTime.Now.ToString("yyyyMMdd")))
- {
- //不存在文件夹则创建
- DirectoryInfo directoryInfo = new DirectoryInfo(pathbase + @"\" + DateTime.Now.ToString("yyyyMMdd"));
- directoryInfo.Create();
- }
- if (!File.Exists(logPath_timer))
- {
- FileStream fs = File.Create(logPath_timer);//创建文件
- fs.Close();
- }
-
- var url = System.Configuration.ConfigurationManager.AppSettings["cyapi"].ToString();
- var arr = url.Split(';');
- foreach (var item in arr)
- {
- try
- {
- System.IO.File.AppendAllText(logPath_timer,
- "\r\n" + DateTime.Now + "\r\n开始访问:" + item);
-
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
- myRequest.KeepAlive = false;
- myRequest.Method = "HEAD"; //设置提交方式可以为"get","head"等
- //myRequest.Timeout = 60000; //设置网页响应时间长度
- myRequest.AllowAutoRedirect = false; //是否允许自动重定向
- HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
- // return (myResponse.StatusCode == HttpStatusCode.OK);//返回响应的状态
- System.IO.File.AppendAllText(logPath_timer,
- "\r\n" + DateTime.Now + "\r\n访问结果:" + myResponse.StatusCode);
- myResponse.Dispose();
- }
- catch (Exception ex)
- {
- System.IO.File.AppendAllText(logPath_timer,
- "\r\n" + DateTime.Now + "\r\n访问结果异常,异常信息:" + ex.Message + ",访问地址:" + item);
- }
- }
-
- }
- catch (Exception ex)
- {
- System.IO.File.AppendAllText(logPath_timer,
- "\r\n" + DateTime.Now + "\r\n访问结果异常,异常信息:" + ex.Message);
- }
- }
- }
- }
|