From 3079f1c651cffc0b3594af62339c06dc8e5caefd Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 2 Jul 2022 20:02:20 +0800 Subject: [PATCH] Improve subscription update --- v2rayN/v2rayN/Base/HttpClientHelper.cs | 24 ++++++------------------ v2rayN/v2rayN/Handler/DownloadHandle.cs | 1 + v2rayN/v2rayN/Handler/UpdateHandle.cs | 17 ++++++++++++++--- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/v2rayN/v2rayN/Base/HttpClientHelper.cs b/v2rayN/v2rayN/Base/HttpClientHelper.cs index ebd58c3d..a3bacb5d 100644 --- a/v2rayN/v2rayN/Base/HttpClientHelper.cs +++ b/v2rayN/v2rayN/Base/HttpClientHelper.cs @@ -45,17 +45,9 @@ namespace v2rayN.Base { return null; } - try - { - HttpResponseMessage response = await httpClient.GetAsync(url); + HttpResponseMessage response = await httpClient.GetAsync(url); - return await response.Content.ReadAsStringAsync(); - } - catch (Exception ex) - { - Utils.SaveLog("GetAsync", ex); - } - return null; + return await response.Content.ReadAsStringAsync(); } public async Task GetAsync(HttpClient client, string url, CancellationToken token) { @@ -63,16 +55,12 @@ namespace v2rayN.Base { return null; } - try - { - HttpResponseMessage response = await client.GetAsync(url, token); - return await response.Content.ReadAsStringAsync(); - } - catch (Exception ex) + HttpResponseMessage response = await client.GetAsync(url, token); + if (!response.IsSuccessStatusCode) { - Utils.SaveLog("GetAsync", ex); + throw new Exception(string.Format("The request returned with HTTP status code {0}", response.StatusCode)); } - return null; + return await response.Content.ReadAsStringAsync(); } public async Task PutAsync(string url, Dictionary headers) diff --git a/v2rayN/v2rayN/Handler/DownloadHandle.cs b/v2rayN/v2rayN/Handler/DownloadHandle.cs index 27595b9e..c2fb9914 100644 --- a/v2rayN/v2rayN/Handler/DownloadHandle.cs +++ b/v2rayN/v2rayN/Handler/DownloadHandle.cs @@ -166,6 +166,7 @@ namespace v2rayN.Handler catch (Exception ex) { Utils.SaveLog(ex.Message, ex); + Error?.Invoke(this, new ErrorEventArgs(ex)); } return null; } diff --git a/v2rayN/v2rayN/Handler/UpdateHandle.cs b/v2rayN/v2rayN/Handler/UpdateHandle.cs index 01038ec4..88ade46d 100644 --- a/v2rayN/v2rayN/Handler/UpdateHandle.cs +++ b/v2rayN/v2rayN/Handler/UpdateHandle.cs @@ -198,20 +198,31 @@ namespace v2rayN.Handler continue; } + var downloadHandle = new DownloadHandle(); + downloadHandle.Error += (sender2, args) => + { + _updateFunc(false, $"{hashCode}{args.GetException().Message}"); + }; + _updateFunc(false, $"{hashCode}{ResUI.MsgStartGettingSubscriptions}"); - var result = await (new DownloadHandle()).DownloadStringAsync(url, blProxy, userAgent); + var result = await downloadHandle.DownloadStringAsync(url, blProxy, userAgent); if (blProxy && Utils.IsNullOrEmpty(result)) { - result = await (new DownloadHandle()).DownloadStringAsync(url, false, userAgent); + result = await downloadHandle.DownloadStringAsync(url, false, userAgent); } - _updateFunc(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}"); if (Utils.IsNullOrEmpty(result)) { _updateFunc(false, $"{hashCode}{ResUI.MsgSubscriptionDecodingFailed}"); } else { + _updateFunc(false, $"{hashCode}{ResUI.MsgGetSubscriptionSuccessfully}"); + if (result.Length < 99) + { + _updateFunc(false, $"{hashCode}{result}"); + } + int ret = ConfigHandler.AddBatchServers(ref config, result, id, groupId); _updateFunc(false, ret > 0