实例,仅供参考,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异常");
            }

        }
    }
}

 

相关文章:

  • 2022-12-23
  • 2021-11-20
  • 2021-11-20
  • 2021-12-26
  • 2022-01-29
  • 2022-01-01
  • 2022-01-03
猜你喜欢
  • 2022-01-08
  • 2021-06-03
  • 2021-10-29
  • 2022-12-23
  • 2021-06-04
  • 2021-10-12
相关资源
相似解决方案