public void TestGetKeyword() { var html = @" <!doctype html> <html lang='en'> <head> <!-- Required meta tags --> <meta charset='utf-8'> <meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'> <link rel='stylesheet' href='https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css'> <title>Hello World</title> </head> <body> <div class='container'> <p> Hello World </p> </div> <script src='https://code.jquery.com/jquery-3.3.1.slim.min.js'></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js'></script> <script src='https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js'></script> </body> </html>"; var keyWord = "Hello World"; MonitoringHelper.EnsureKeyWordExists(html, keyWord); }
private async Task KeyWordMonitor(IEnumerable <Monitor> keyWordMonitors) { foreach (var monitor in keyWordMonitors) { monitor.LastMonitorDate = DateTime.UtcNow; using (var httpClient = new HttpClient()) { var monitorLog = new MonitorLog { MonitorId = monitor.Id }; var responseCode = 0; try { httpClient.BaseAddress = new Uri(monitor.Url); httpClient.DefaultRequestHeaders.Accept.Clear(); var response = await httpClient.GetAsync(""); responseCode = (int)response.StatusCode; response.EnsureSuccessStatusCode(); using (var content = response.Content) { var html = await content.ReadAsStringAsync(); MonitoringHelper.EnsureKeyWordExists(html, monitor.KeyWord); monitor.LastMonitorSuccess = true; monitorLog.ResponseCode = responseCode; monitorLog.Successful = true; monitor.Triggered = false; } } catch (KeyWordNotFoundException) { monitorLog.ResponseCode = responseCode; monitorLog.Successful = false; monitorLog.ExceptionMessage = "Unable to find keyword"; monitor.LastMonitorSuccess = false; _failedMonitors.Add(new Monitor(monitor)); monitor.Triggered = true; } catch (Exception ex) { monitorLog.ResponseCode = responseCode; monitorLog.Successful = false; monitorLog.ExceptionMessage = ex.Message; monitor.LastMonitorSuccess = false; _failedMonitors.Add(new Monitor(monitor)); monitor.Triggered = true; } finally { _processedMonitors.Add(monitor); _monitorLogs.Add(monitorLog); } } } }