肖洋
2024-11-22 2e5d53bdde2df2419f879d4acad8f39fdbcf5b1a
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs
@@ -13,10 +13,10 @@
    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);
        LogRequestParameters(parameters, url);
        // 将参数拼接到URL中
        string queryString = string.Join("&", parameters.Select(x => $"{x.Key}={x.Value}"));
@@ -35,7 +35,7 @@
            string responseBody = await response.Content.ReadAsStringAsync();
            // 记录响应参数
            LogResponseParameters(responseBody);
            LogResponseParameters(responseBody, url);
            // 返回响应内容
            return responseBody;
@@ -43,19 +43,24 @@
    }
    // 封装一个用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);
        LogRequestParameters(parameters, url);
        // 创建HttpClient实例
        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();
@@ -64,20 +69,29 @@
            string responseBody = await response.Content.ReadAsStringAsync();
            // 记录响应参数
            LogResponseParameters(responseBody);
            LogResponseParameters(responseBody, url);
            // 返回响应内容
            return responseBody;
        }
    }
    private static void LogRequestParameters(Dictionary<string, string> parameters)
    private static void LogRequestParameters(Dictionary<string, object> parameters,string url = "")
    {
        LogFactory.GetLog("API接口").Info(true, "请求参数: " + JsonConvert.SerializeObject(parameters));
        LogFactory.GetLog("API接口").Info(true, "url:" + url + "请求参数: " + JsonConvert.SerializeObject(parameters));
    }
    private static void LogResponseParameters(string responseBody)
    private static void LogResponseParameters(string responseBody, string url = "")
    {
        LogFactory.GetLog("API接口").Info(true, "响应参数: " + responseBody);
        LogFactory.GetLog("API接口").Info(true, "url:" + url + "响应参数: " + 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());
        }
    }
}