刘磊
2024-11-15 e7639b1edab8ae2921b7f9545271dfadf1566d63
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs
@@ -13,7 +13,7 @@
    private static readonly LogFactory LogFactory = new LogFactory();
    // 封装一个用HttpClient发送GET请求的方法有参数
    public static async Task<string> GetAsync(string url, Dictionary<string, string> parameters)
    public static async Task<string> GetAsync(string url, Dictionary<string, object> parameters)
    {
        // 记录请求参数
        LogRequestParameters(parameters);
@@ -43,7 +43,7 @@
    }
    // 封装一个用HttpClient发送Post请求的方法有参数
    public static async Task<string> PostAsync(string url, Dictionary<string, string> parameters)
    public static async Task<string> PostAsync(string url, Dictionary<string, object> parameters)
    {
        // 记录请求参数
        LogRequestParameters(parameters);
@@ -52,10 +52,15 @@
        using (HttpClient client = new HttpClient())
        {
            // 将参数转换为FormUrlEncodedContent
            FormUrlEncodedContent content = new FormUrlEncodedContent(parameters);
            string content = JsonConvert.SerializeObject(parameters);
            // 发送POST请求并获取响应
            HttpResponseMessage response = await client.PostAsync(url, content);
            var request = new HttpRequestMessage(HttpMethod.Post, url);
            request.Content = new StringContent(content, Encoding.UTF8, "application/json");
            //var content = new FormUrlEncodedContent(ConvertToKeyValuePairs(parameters));
            //// 发送POST请求并获取响应
            //HttpResponseMessage response = await client.PostAsync(url, content);
            HttpResponseMessage response = await client.SendAsync(request);
            // 确保响应成功
            response.EnsureSuccessStatusCode();
@@ -71,7 +76,7 @@
        }
    }
    private static void LogRequestParameters(Dictionary<string, string> parameters)
    private static void LogRequestParameters(Dictionary<string, object> parameters)
    {
        LogFactory.GetLog("API接口").Info(true, "请求参数: " + JsonConvert.SerializeObject(parameters));
    }
@@ -80,4 +85,13 @@
    {
        LogFactory.GetLog("API接口").Info(true, "响应参数: " + responseBody);
    }
    private static IEnumerable<KeyValuePair<string, string>> ConvertToKeyValuePairs(Dictionary<string, object> parameters)
    {
        foreach (var kvp in parameters)
        {
            yield return new KeyValuePair<string, string>(kvp.Key, kvp.Value?.ToString());
        }
    }
}