修正了SimApiHttpClient初始化方式

This commit is contained in:
2026-05-05 03:53:02 +08:00
parent 3b263b4563
commit 140e039d27
2 changed files with 16 additions and 11 deletions
+2 -2
View File
@@ -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, "未知错误代码"))
{
}
+14 -9
View File
@@ -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<string>
{
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey)
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), AppKey)
};
return Query<T>(url, req);
}
@@ -82,7 +87,7 @@ public class SimApiHttpClient(string? appId, string appKey, bool debug = false)
{
var req = new SimApiOneFieldRequest<string>
{
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey)
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), AppKey)
};
return SignQuery<T>(url, req, queries);
}
@@ -98,13 +103,13 @@ public class SimApiHttpClient(string? appId, string appKey, bool debug = false)
private T? Query<T>(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");
}