From 140e039d277bec8006bcbb5f24bf22eb2b8bce4b Mon Sep 17 00:00:00 2001 From: xRain Date: Tue, 5 May 2026 03:53:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86SimApiHttpClient?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Communications/SimApiBaseResponse.cs | 4 ++-- Helpers/SimApiHttpClient.cs | 23 ++++++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Communications/SimApiBaseResponse.cs b/Communications/SimApiBaseResponse.cs index 6c48fd6..0e6f073 100644 --- a/Communications/SimApiBaseResponse.cs +++ b/Communications/SimApiBaseResponse.cs @@ -27,11 +27,11 @@ public class SimApiBaseResponse(int code = 200, string message = "成功") { 400, "参数错误" }, { 401, "需要登录" }, { 403, "无权访问" }, - { 404, "接口不存在" }, + { 404, "请求资源不存在" }, { 500, "服务器错误" } }; - public SimApiBaseResponse(int code) : this(code, MsgBox.GetValueOrDefault(code, "未知错误")) + public SimApiBaseResponse(int code) : this(code, MsgBox.GetValueOrDefault(code, "未知错误代码")) { } diff --git a/Helpers/SimApiHttpClient.cs b/Helpers/SimApiHttpClient.cs index cd4576d..c494ee4 100644 --- a/Helpers/SimApiHttpClient.cs +++ b/Helpers/SimApiHttpClient.cs @@ -8,9 +8,14 @@ using SimApi.Exceptions; namespace SimApi.Helpers; -public class SimApiHttpClient(string? appId, string appKey, bool debug = false) +public class SimApiHttpClient { - public string Server { get; init; } = string.Empty; + public required string Server { get; init; } + public required string AppId { get; init; } + + public required string AppKey { get; init; } + + public bool Debug { get; init; } = false; public string SignName { get; init; } = "sign"; public string TimestampName { get; init; } = "timestamp"; public string NonceName { get; init; } = "nonce"; @@ -32,11 +37,11 @@ public class SimApiHttpClient(string? appId, string appKey, bool debug = false) (current, signField) => current + $"{signField}={queries?[signField]}&"); if (!string.IsNullOrEmpty(AppIdName)) { - queryUrl += $"{AppIdName}={appId}&"; + queryUrl += $"{AppIdName}={AppId}&"; } queryUrl += $"{TimestampName}={(int)SimApiUtil.TimestampNow}&{NonceName}={Guid.NewGuid()}"; - var signStr = $"{queryUrl}&{appKey}"; + var signStr = $"{queryUrl}&{AppKey}"; var path = $"{url}?{queryUrl}&{SignName}={SimApiUtil.Md5(signStr)}"; if (queries != null) @@ -60,12 +65,12 @@ public class SimApiHttpClient(string? appId, string appKey, bool debug = false) url = Server + url; if (!string.IsNullOrEmpty(AppIdName)) { - url += $"?{AppIdName}={appId}"; + url += $"?{AppIdName}={AppId}"; } var req = new SimApiOneFieldRequest { - Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey) + Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), AppKey) }; return Query(url, req); } @@ -82,7 +87,7 @@ public class SimApiHttpClient(string? appId, string appKey, bool debug = false) { var req = new SimApiOneFieldRequest { - Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey) + Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), AppKey) }; return SignQuery(url, req, queries); } @@ -98,13 +103,13 @@ public class SimApiHttpClient(string? appId, string appKey, bool debug = false) private T? Query(string url, object? req) { var http = new HttpClient(); - if (debug) + if (Debug) { Console.WriteLine($"[HTTPCLIENT请求] {url}\n{SimApiUtil.Json(req)}\n"); } var resp = http.PostAsJsonAsync(url, req).Result; - if (debug) + if (Debug) { Console.WriteLine($"[HTTPCLIENT响应] {resp.Content.ReadAsStringAsync().Result}\n"); }