/// <summary> /// This contructor is created when adding the Monitor the first time /// </summary> /// <param name="url"></param> /// <param name="userAgent"></param> public Monitor(string url, string userAgent, List<IMonitorRule> ruleList) { this.request = (HttpWebRequest)WebRequest.Create(url); this.userAgent = userAgent; this.lastUpdated = DateTime.Now; this.url = url; this.id = Guid.NewGuid(); this.running = false; this.rules = new MonitorRules(this); this.infoText = "Monitor hasn't been started yet"; this.pingNo = 0; this.totaltPingTime = 0; this.history = new List<MonitorHistoryItem>(historySize); this.colorAsInt = Color.Blue.ToArgb(); this.RuleList = ruleList; this.monitorPingResult = new MonitorPingResult(); //HttpRequest fields this.statusCode = ""; this.statusDescription = ""; this.responseUri = ""; this.responseContentType = ""; this.responseHtml = ""; this.username = ""; this.password = ""; this.proxy = new WebProxy(); }
public MonitorPingResult Ping() { HttpWebRequest request = null; MonitorPingResult result = null; try { //************ SETUP REQUEST ****************// result = new MonitorPingResult(); request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "HEAD"; //According to this, we should use HEAD in most cases: http://www.dscoduc.com/2009/04/choose-your-http-requests-wisely/ //************ HANDLE RESPONSE ****************// // Create a new 'HttpWebRequest' Object to the mentioned URL. OnPinging(); HttpWebResponse myHttpWebResponse = (HttpWebResponse)request.GetResponse(); OnPingCompleted(); // Check if StatusCode is OK, else throw exception if (myHttpWebResponse.StatusCode != HttpStatusCode.OK) throw new Exception(myHttpWebResponse.StatusDescription); //Run through all the monitors plugins, so they can act upon the succeeded request foreach (IMonitorPlugin plugin in Plugins) { plugin.Execute(this, request); } //Everything went good result.Succeeded = true; } catch (Exception exp) { result.Error = exp; result.Succeeded = false; //Todo: Not suere this is the right place to put this OnPingFailed(); } finally { //Clean up if (request != null) request.Abort(); request = null; } return result; }