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)