实例,仅供参考,WebApiResult为统一封装返回值
public class KeyWordServiceProxy { private HttpClient _httpclient; private IConfiguration _config; private ILogger _logger; public KeyWordServiceProxy(HttpClient httpClient, IConfiguration config, ILogger<KeyWordServiceProxy> logger) { _config = config; httpClient.BaseAddress = new Uri(_config[RiskApi.Model.StringConstant.ResourceApi.TrimEnd('/')]); _httpclient = httpClient; _logger = logger; } /// <summary> /// 添加 /// </summary> /// <param name="param"></param> /// <returns></returns> public async Task<WebApiResult> AddSensitive(SenApiAddRequest param) { try { _logger.LogInformation($"KeyWordServiceProxy-AddSensitive...,request param:{JsonConvert.SerializeObject(param)}"); HttpContent postContent = new StringContent(JsonConvert.SerializeObject(param)); postContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var requestUri = "/api/***/AddSen"; var response = await _httpclient.PostAsync(requestUri, postContent); _logger.LogInformation($"KeyWordServiceProxy-AddSensitive...,response:{JsonConvert.SerializeObject(response)}"); if (response.IsSuccessStatusCode) { var responseStr = await response.Content.ReadAsStringAsync(); var responseObj = JsonConvert.DeserializeObject<WebApiResult>(responseStr); return responseObj; } return new WebApiResult(ApiResultCode.Fail, "保存失败"); } catch (Exception ex) { _logger.LogInformation($"KeyWordServiceProxy-AddSensitive...,ex:{ex.Message + ex?.InnerException}"); throw new Exception("AddSensitive异常"); } } } }