From bb162d6ffd647c3adec89789f2f2004dca513513 Mon Sep 17 00:00:00 2001 From: textGamex Date: Sun, 21 Jul 2024 19:24:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20VersionService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Domain/Service/VersionService.cs | 84 +++++++++++++------ 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/src/c#/GeneralUpdate.Core/Domain/Service/VersionService.cs b/src/c#/GeneralUpdate.Core/Domain/Service/VersionService.cs index 3da4cd72..4c3af355 100644 --- a/src/c#/GeneralUpdate.Core/Domain/Service/VersionService.cs +++ b/src/c#/GeneralUpdate.Core/Domain/Service/VersionService.cs @@ -4,68 +4,100 @@ using System; using System.IO; using System.Net; +using System.Net.Http; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; +using GeneralUpdate.Core.Events; +using GeneralUpdate.Core.Events.CommonArgs; namespace GeneralUpdate.Core.Domain.Service { public class VersionService { - public async Task ValidationVersion(string url, Action statusCallback) - { - statusCallback(this, ProgressType.Check, "Update checking..."); - VersionRespDTO resp = await ValidationVersion(url); - if (resp == null) statusCallback(this, ProgressType.Check, $"Request failed , Code :{resp.Code}, Message:{resp.Message} !"); - return await ValidationVersion(url); - } - public async Task ValidationVersion(string url) { var updateResp = await GetTaskAsync(url); - if (updateResp == null || updateResp.Body == null) throw new ArgumentNullException($"The verification request is abnormal, please check the network or parameter configuration!"); - if (updateResp.Code != HttpStatus.OK) throw new Exception($"Request failed , Code :{updateResp.Code}, Message:{updateResp.Message} !"); - if (updateResp.Code == HttpStatus.OK) return updateResp; - return null; + if (updateResp == null || updateResp.Body == null) + { + throw new ArgumentNullException( + nameof(updateResp), + "The verification request is abnormal, please check the network or parameter configuration!" + ); + } + + if (updateResp.Code == HttpStatus.OK) + { + return updateResp; + } + else + { + throw new WebException( + $"Request failed , Code :{updateResp.Code}, Message:{updateResp.Message} !" + ); + } } - public static async Task GetTaskAsync(string http_url, string header_key = null, string header_value = null) + private async Task GetTaskAsync( + string httpUrl, + string headerKey = null, + string headerValue = null + ) { HttpWebResponse response = null; try { - ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); - string httpUri = http_url; - var encoding = Encoding.GetEncoding("utf-8"); - var request = (HttpWebRequest)WebRequest.Create(httpUri); + ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; + var request = (HttpWebRequest)WebRequest.Create(httpUrl); request.Method = "GET"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/x-www-form-urlencoded"; request.Timeout = 15000; - if (!string.IsNullOrEmpty(header_key) && !string.IsNullOrEmpty(header_value)) + if (!string.IsNullOrEmpty(headerKey) && !string.IsNullOrEmpty(headerValue)) { - request.Headers[header_key] = header_value; + request.Headers[headerKey] = headerValue; } response = (HttpWebResponse)await request.GetResponseAsync(); - if (response.StatusCode != HttpStatusCode.OK) return default(T); - using (var reader = new StreamReader(response.GetResponseStream(), encoding)) + if (response.StatusCode != HttpStatusCode.OK) + { + throw new WebException( + $"Response status code does not indicate success: {response.StatusCode}!" + ); + } + var responseStream = response.GetResponseStream(); + if (responseStream == null) + { + throw new WebException( + "Response stream is null, please check the network or parameter configuration!" + ); + } + using (var reader = new StreamReader(responseStream, Encoding.UTF8)) { - var tempStr = reader.ReadToEnd(); + var tempStr = await reader.ReadToEndAsync(); var respContent = JsonConvert.DeserializeObject(tempStr); return respContent; } } - catch + catch (Exception ex) { - return default(T); + EventManager.Instance.Dispatch>( + this, + new ExceptionEventArgs(ex) + ); + return default; } finally { - if (response != null) response.Close(); + response?.Close(); } } - private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true; + private static bool CheckValidationResult( + object sender, + X509Certificate certificate, + X509Chain chain, + SslPolicyErrors sslPolicyErrors + ) => true; } } \ No newline at end of file