From aed49e4e42ce2c4a76027d0968f9d0fb4d038fde Mon Sep 17 00:00:00 2001 From: huiyadanli Date: Sat, 7 Dec 2019 14:38:16 +0800 Subject: [PATCH] =?UTF-8?q?[#]=20=E4=BF=AE=E6=94=B9=E8=A1=A5=E4=B8=81?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=96=B9=E5=BC=8F,=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=96=B0=E7=9A=84=E5=9C=B0=E5=9D=80=E5=92=8C=E8=A1=A5=E4=B8=81?= =?UTF-8?q?=E5=8F=91=E5=B8=83=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RevokeMsgPatcher/FormMain.cs | 25 +--------------- RevokeMsgPatcher/Utils/HttpUtil.cs | 47 ++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/RevokeMsgPatcher/FormMain.cs b/RevokeMsgPatcher/FormMain.cs index f993322..08f183e 100644 --- a/RevokeMsgPatcher/FormMain.cs +++ b/RevokeMsgPatcher/FormMain.cs @@ -210,11 +210,10 @@ namespace RevokeMsgPatcher private async void FormMain_Load(object sender, EventArgs e) { // 异步获取最新的补丁信息 - string json = await GetPathJsonAsync(); + string json = await HttpUtil.GetPatchJsonAsync(); if (string.IsNullOrEmpty(json)) { lblUpdatePachJson.Text = "[ 获取最新补丁信息失败 ]"; - } else { @@ -247,28 +246,6 @@ namespace RevokeMsgPatcher } } - private async Task GetPathJsonAsync() - { - string downStr = null; - try - { - downStr = await HttpUtil.Client.GetStringAsync("https://huiyadanli.coding.me/i/revokemsg/05.json"); - } - catch (Exception ex1) - { - Console.WriteLine(ex1.Message); - try - { - downStr = await HttpUtil.Client.GetStringAsync("https://www.huiyadan.com/i/revokemsg/05.json"); - } - catch (Exception ex2) - { - Console.WriteLine(ex2.Message); - } - } - return downStr; - } - private void lblUpdatePachJson_Click(object sender, EventArgs e) { string tips = ""; diff --git a/RevokeMsgPatcher/Utils/HttpUtil.cs b/RevokeMsgPatcher/Utils/HttpUtil.cs index d67e16e..90972d2 100644 --- a/RevokeMsgPatcher/Utils/HttpUtil.cs +++ b/RevokeMsgPatcher/Utils/HttpUtil.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; +using System.Net; using System.Net.Http; -using System.Text; using System.Threading.Tasks; namespace RevokeMsgPatcher.Utils @@ -10,5 +8,48 @@ namespace RevokeMsgPatcher.Utils public class HttpUtil { public static HttpClient Client { get; } = new HttpClient(); + + static HttpUtil() + { + Client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"); + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; + } + + /// + /// 补丁路径 + /// 已经弃用的路径 + /// https://swordmaker-hauls-51508.netlify.com/i/revokemsg/05.json + /// https://huiyadanli.github.io/i/revokemsg/05.json + /// + private static readonly string[] urls = new string[] + { + "https://coding.net/u/huiyadanli/p/RevokeMsgPatcher/git/raw/master/RevokeMsgPatcher.Assistant/Data/0.6/patch.json", + "https://gitee.com/huiyadanli/RevokeMsgPatcher/raw/master/RevokeMsgPatcher.Assistant/Data/0.6/patch.json", + "https://raw.githubusercontent.com/huiyadanli/RevokeMsgPatcher/master/RevokeMsgPatcher.Assistant/Data/0.6/patch.json" + }; + + private static int i = 0; + + public static async Task GetPatchJsonAsync() + { + try + { + return await Client.GetStringAsync(urls[i]); + } + catch (Exception ex) + { + Console.WriteLine("第" + (i + 1) + "次请求异常:[" + ex.Message + "]\nURL:" + urls[i]); + i++; + if (i > urls.Length) + { + i = 0; + return null; + } + else + { + return await GetPatchJsonAsync(); + } + } + } } }