From 1121a0d0b52e5761cf2cb583fb76db58a9b0d01a Mon Sep 17 00:00:00 2001 From: milimoe Date: Sat, 3 Jan 2026 21:29:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9F=BA=E7=A1=80=20GET=20?= =?UTF-8?q?=E5=92=8C=20POST?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Utility/General.cs | 38 ++++++++++++++++++++++------ Library/Common/Network/HTTPClient.cs | 28 +++++++++++++++++--- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/Api/Utility/General.cs b/Api/Utility/General.cs index d9c86b3..0091d43 100644 --- a/Api/Utility/General.cs +++ b/Api/Utility/General.cs @@ -256,8 +256,10 @@ namespace Milimoe.FunGame.Core.Api.Utility /// public static T? JsonDeserializeFromHashtable(Hashtable hashtable, string key, JsonSerializerOptions options) => Service.JsonManager.GetObject(hashtable, key, options); - // 创建一个静态 HttpClient 实例,供整个应用程序生命周期使用 - private static readonly HttpClient client = new(); + /// + /// 创建一个静态 HttpClient 实例,供整个应用程序生命周期使用 + /// + public static HttpClient HttpClient { get; } = new(); /// /// 发送 GET 请求 @@ -266,12 +268,22 @@ namespace Milimoe.FunGame.Core.Api.Utility /// public static async Task HttpGet(string url) { - HttpResponseMessage response = await client.GetAsync(url); + HttpResponseMessage response = await HttpClient.GetAsync(url); response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); - T? result = JsonDeserialize(content); - return result; + try + { + return JsonDeserialize(content); + } + catch + { + if (typeof(T) == typeof(string)) + { + return (T)(object)content; + } + } + return default; } /// @@ -284,12 +296,22 @@ namespace Milimoe.FunGame.Core.Api.Utility public static async Task HttpPost(string url, string json) { HttpContent content = new StringContent(json, General.DefaultEncoding, "application/json"); - HttpResponseMessage response = await client.PostAsync(url, content); + HttpResponseMessage response = await HttpClient.PostAsync(url, content); response.EnsureSuccessStatusCode(); string read = await response.Content.ReadAsStringAsync(); - T? result = JsonDeserialize(read); - return result; + try + { + return JsonDeserialize(read); + } + catch + { + if (typeof(T) == typeof(string)) + { + return (T)(object)content; + } + } + return default; } } diff --git a/Library/Common/Network/HTTPClient.cs b/Library/Common/Network/HTTPClient.cs index 0493a65..5d01337 100644 --- a/Library/Common/Network/HTTPClient.cs +++ b/Library/Common/Network/HTTPClient.cs @@ -89,8 +89,18 @@ namespace Milimoe.FunGame.Core.Library.Common.Network response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); - T? result = NetworkUtility.JsonDeserialize(content); - return result; + try + { + return NetworkUtility.JsonDeserialize(content); + } + catch + { + if (typeof(T) == typeof(string)) + { + return (T)(object)content; + } + } + return default; } /// @@ -107,8 +117,18 @@ namespace Milimoe.FunGame.Core.Library.Common.Network response.EnsureSuccessStatusCode(); string read = await response.Content.ReadAsStringAsync(); - T? result = NetworkUtility.JsonDeserialize(read); - return result; + try + { + return NetworkUtility.JsonDeserialize(read); + } + catch + { + if (typeof(T) == typeof(string)) + { + return (T)(object)content; + } + } + return default; } public void AddSocketObjectHandler(Action method)