diff --git a/Extensions.cs b/Extensions.cs index dca887f..ac22ea2 100644 --- a/Extensions.cs +++ b/Extensions.cs @@ -6,5 +6,15 @@ public static int GetEpoch(this DateTime span) { double t = (span - new DateTime(1970, 1, 1)).TotalSeconds; return (int) t; } + + public static bool CheckSuccess(this HttpResponseMessage msg) { + try { + msg.EnsureSuccessStatusCode(); + } + catch { + return false; + } + return true; + } } } \ No newline at end of file diff --git a/SophosSession.cs b/SophosSession.cs index fd306e5..b600d39 100644 --- a/SophosSession.cs +++ b/SophosSession.cs @@ -24,6 +24,8 @@ public class SophosSession private int _goodRequests; private int _failedRequests; + public int TotalSessionRequests { get => _goodRequests + _failedRequests; } + public SophosSession(SophosSessionConfiguration config) { _username = System.Web.HttpUtility.UrlEncode(config.Username); @@ -63,7 +65,6 @@ public async Task Login() { throw new WebException("Request failed: " + msg.StatusCode); } - _client.DefaultRequestHeaders.Remove("X-Requested-With"); } @@ -83,8 +84,7 @@ public async Task KeepAlive() { ThreadPool.QueueUserWorkItem(async (o) => { while (true) { await Task.Delay(300000); - ConsoleLogger.LogInfo($"Sent {_goodRequests + _failedRequests} heartbeats in the past five minutes."); - ConsoleLogger.LogInfo($"{_goodRequests}/{_failedRequests + _goodRequests} succeeded"); + ConsoleLogger.LogInfo($"Sent {TotalSessionRequests} heartbeats in the past five minutes. {_goodRequests}/{TotalSessionRequests} succeeded"); _goodRequests = 0; _failedRequests = 0; } @@ -93,7 +93,7 @@ public async Task KeepAlive() { while (true) { await Task.Delay(_heartbeatTimeout); msg = await _client.PostAsync(url, new FormUrlEncodedContent(new Dictionary())); - if (msg.StatusCode != HttpStatusCode.OK) + if (!msg.CheckSuccess()) _failedRequests++; else _goodRequests++;