Skip to content

Commit

Permalink
Dispose webclient. Use autoproperty
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvanpoppel committed Apr 11, 2020
1 parent 466dd4a commit 50219ac
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions KeeTrayTOTP/Libraries/TimeCorrectionProvider.cs
Expand Up @@ -31,11 +31,10 @@ public class TimeCorrectionProvider
public static int Interval { get { return _interval; } set { _interval = value; } }
private long _intervalStretcher;

private volatile readonly string _url;
/// <summary>
/// Returns the URL this instance is using to checks for time correction.
/// </summary>
public string Url { get { return _url; } }
public string Url { get; private set; }

private TimeSpan _timeCorrection;
/// <summary>
Expand All @@ -62,12 +61,12 @@ public class TimeCorrectionProvider
/// <param name="enable">Enable or disable the time correction check.</param>
public TimeCorrectionProvider(string url, bool enable = true)
{
if (url == string.Empty)
if (string.IsNullOrEmpty(url))
{
throw new Exception("Invalid URL."); //Throws exception if the URL is invalid as the class cannot work without it.
}

_url = url; //Defines variable from argument.
Url = url; //Defines variable from argument.
_enable = enable; //Defines variable from argument.
_lastUpdateDateTime = DateTime.MinValue; //Defines variable from non-constant default value.
_timeCorrection = TimeSpan.Zero; //Defines variable from non-constant default value.
Expand Down Expand Up @@ -117,11 +116,13 @@ private void Task_Thread()
{
try
{
var webClient = new System.Net.WebClient(); //WebClient to connect to server.
webClient.DownloadData(_url); //Downloads the server's page using HTTP or HTTPS.
var dateHeader = webClient.ResponseHeaders.Get("Date"); //Gets the date from the HTTP header of the downloaded page.
_timeCorrection = DateTime.UtcNow - DateTime.Parse(dateHeader, System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat).ToUniversalTime(); //Compares the downloaded date to the systems date giving us a timespan.
_lastUpdateSucceeded = true; //Informs that the date check has succeeded.
using (var webClient = new System.Net.WebClient())
{
webClient.DownloadData(Url); //Downloads the server's page using HTTP or HTTPS.
var dateHeader = webClient.ResponseHeaders.Get("Date"); //Gets the date from the HTTP header of the downloaded page.
_timeCorrection = DateTime.UtcNow - DateTime.Parse(dateHeader, System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat).ToUniversalTime(); //Compares the downloaded date to the systems date giving us a timespan.
_lastUpdateSucceeded = true; //Informs that the date check has succeeded.
}
}
catch (Exception)
{
Expand Down

0 comments on commit 50219ac

Please sign in to comment.