/// <summary> /// Вход в программу /// </summary> /// <param name="args">Аргументы</param> static void Main(string[] args) { var consoleHelper = new ConsoleHelper(); if (args == null || args.Length < 2) { consoleHelper.WriteMessageWithTimeStamp("Не хватает аргументов при запуске", ConsoleColor.DarkRed); return; } var httpMethodConverterList = new HttpMethodConverterList(); var httpMethodConverter = new HttpMethodConverter(httpMethodConverterList); var method = httpMethodConverter.GetMethod(args[0]); var url = args[1]; var jsonConverter = new JsonConvertAdapter(); var requestHelper = new RequestHelper(jsonConverter); var login = Properties.Settings.Default.login; var password = Properties.Settings.Default.password; try { consoleHelper.WriteMessageWithTimeStamp($"Посылаем {args[0].ToUpper()} запрос на адрес {url}"); var content = args.Length > 2 && args[2] != null && (method == HttpMethod.Post || method == HttpMethod.Put) ? args[2] : null; object responseObject = requestHelper.SendRequest <object>(method, url, content, login, password).Result; consoleHelper.WriteMessageWithTimeStamp($"Сервер вернул ответ:\n{responseObject}", ConsoleColor.DarkGreen); } catch (Exception e) { consoleHelper.WriteMessageWithTimeStamp($"{e.Message}\n{e}", ConsoleColor.Red); } }
/// <summary> /// Handles the signaling of this <see cref="NetWatcher"/>. /// </summary> protected override void HandleSignaling() { lock (_locker) { string currentSignature = SignatureDefault; string listenerHeader = string.Format(CultureInfo.InvariantCulture, "Cuemon.Net.NetWatcher; Interval={0} seconds", Period.TotalSeconds); DateTime utcLastModified = DateTime.UtcNow; switch (Scheme) { case UriScheme.File: utcLastModified = File.GetLastWriteTimeUtc(RequestUri.LocalPath); if (CheckResponseData) { using (FileStream stream = new FileStream(RequestUri.LocalPath, FileMode.Open, FileAccess.Read)) { stream.Position = 0; currentSignature = HashUtility.ComputeHash(stream).ToHexadecimal(); } } break; case UriScheme.Http: case UriScheme.Https: using (HttpManager manager = new HttpManager()) { manager.DefaultRequestHeaders.Add("Listener-Object", listenerHeader); using (HttpResponseMessage response = CheckResponseData ? manager.HttpGetAsync(RequestUri).Result : manager.HttpHeadAsync(RequestUri).Result) { switch (HttpMethodConverter.ToHttpMethod(response.RequestMessage.Method)) { case HttpMethods.Get: var etag = response.Headers.ETag; currentSignature = string.IsNullOrEmpty(etag.Tag) ? HashUtility.ComputeHash(response.Content.ReadAsByteArrayAsync().Result).ToHexadecimal() : etag.Tag; break; case HttpMethods.Head: utcLastModified = response.Content.Headers.LastModified.HasValue ? response.Content.Headers.LastModified.Value.UtcDateTime : DateTime.MaxValue; break; } } } break; default: throw new InvalidOperationException("Only allowed schemes for now is File, HTTP or HTTPS."); } if (CheckResponseData) { if (Signature == SignatureDefault) { Signature = currentSignature; } if (!Signature.Equals(currentSignature, StringComparison.OrdinalIgnoreCase)) { SetUtcLastModified(utcLastModified); OnChangedRaised(); } Signature = currentSignature; return; } if (utcLastModified > UtcCreated) { SetUtcLastModified(utcLastModified); OnChangedRaised(); } } }