private static async Task<TResult> DeleteData<TParam, TResult>(string url, TParam param)
            where TParam : class
            where TResult : class
        {
            try
            {
                var json = JsonConvert.SerializeObject(param, s_JsonSerializerSettings);

                s_Logger.LogInformation($"发送请求到{url},发送的数据为{json}");
                using (var client = new HttpClient())
                using (var request = new HttpRequestMessage(HttpMethod.Delete, url))
                using (request.Content = new StringContent(json, Encoding.UTF8, "application/json"))
                {
                    var httpResponse = await client.SendAsync(request).ConfigureAwait(false);
                    if (httpResponse.IsSuccessStatusCode)
                    {
                        var responseBody = await httpResponse.Content.ReadAsStringAsync();
                        s_Logger.LogInformation($"请求完成,结果为“{responseBody}”");

                        return JsonConvert.DeserializeObject<TResult>(responseBody);
                    }
                    else
                    {
                        throw new Exception($"响应状态为{httpResponse.StatusCode}");
                    }
                }
            }
            catch (Exception ex)
            {
                s_Logger.LogError(ex, $"请求失败,错误为{ex.Message}");
                return null;
            }
        }

 

相关文章:

  • 2022-12-23
  • 2021-12-24
  • 2022-01-17
  • 2022-12-23
  • 2021-05-04
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2021-04-19
  • 2022-12-23
相关资源
相似解决方案