1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.IO;
using System.Net;
namespace MonitorWebSite
{
public partial class MonitorWebSite : ServiceBase
{
private Timer t = null;
public MonitorWebSite()
{
InitializeComponent();
t = new Timer(10000);
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
}
void t_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
// Send the HTTP request
string url = "http://www.microsoft.com";
HttpWebRequest g = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse r = (HttpWebResponse)g.GetResponse();
// Log the response to a text file
string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "log.txt";
TextWriter tw = new StreamWriter(path, true);
tw.WriteLine(DateTime.Now.ToString() + " for " + url + ": " + r.StatusCode.ToString());
tw.Close();
// Close the HTTP response
r.Close();
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Application",
"Exception: " + ex.Message.ToString());
}
}
#region OnStart, OnStop, OnContinue, et OnShutdown méthodes
protected override void OnStart(string[] args)
{
t.Start();
}
protected override void OnStop()
{
t.Stop();
}
protected override void OnContinue()
{
t.Start();
}
protected override void OnPause()
{
t.Stop();
}
protected override void OnShutdown()
{
t.Stop();
}
#endregion
}
} |