zoukankan      html  css  js  c++  java
  • .Net中通过HttpClient发送Delete请求附带Body参数

    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;
                }
            }
  • 相关阅读:
    电赛小结
    markdown小结
    一元运算符重载
    二维数组作为函数参数传递剖析(转载)
    C语言内存(转载)
    Effective C++ chapter1:Accustiming Yourself to C++
    C++ 模板
    const
    命令行参数
    AStar算法
  • 原文地址:https://www.cnblogs.com/MrZheng/p/15402831.html
Copyright © 2011-2022 走看看