C#调用HTTP接口类

    public class Response<T>
    {
        public bool IsSuccess { get; set; }
        public string Message { get; set; }
        public T Ins { get; set; }       
    }

    public class Response
    {
        public int status { get; set; }
        public bool success { get; set; }
        public string msg { get; set; }
    }

    public class HttpService
    {
        HttpClient _Client;

        string IP;
        string BaseUri;
        public HttpService(HttpClient httpClient)
        {
            _Client = httpClient;
            _Client.DefaultRequestHeaders.Accept.Clear();
            _Client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            _Client.Timeout = new TimeSpan(0, 0, 10);
            BaseUri = $"http://192.168.1.10:9000/api/";

        }    
        public void ChangeIp(string ip)
        {
            IP = ip;
            //IP = "192.168.1.13";
            BaseUri = $"http://{IP}:9000/api/";
        }
        JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions()
        {
            Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
        };
        public async Task<Response<T>> GetAsync<T>(string uri)
        {           
            string full_uri = BaseUri+uri;
            Response<T> response = new Response<T>();
            response.IsSuccess = true;
            try
            {
                var responseM = await _Client.GetAsync(full_uri);
                responseM.EnsureSuccessStatusCode();
                string responseBody = await responseM.Content.ReadAsStringAsync();
                response.Ins = JsonSerializer.Deserialize<T>(responseBody);
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message = ex.Message;  
            }
            return response;
        }
        public async Task<Response<T>> DeleteAsync<T>(string uri)
        {
            string full_uri = BaseUri + uri;
            Response<T> response = new Response<T>();
            response.IsSuccess = true;
            try
            {
                var responseM = await _Client.DeleteAsync(full_uri);
                responseM.EnsureSuccessStatusCode();              
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message = ex.Message;
            }
            return response;
        }
        public async Task<Response<T>> PutAsync<T>(string uri,T ins)
        {
            string full_uri = BaseUri + uri;
            Response<T> response = new Response<T>();
            response.IsSuccess = true;

            var json=JsonSerializer.Serialize(ins, jsonSerializerOptions);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            string msg = string.Empty;
            try
            {
                var responseM = await _Client.PutAsync(full_uri, content);
                msg = await responseM.Content.ReadAsStringAsync();
                responseM.EnsureSuccessStatusCode();             
            }
            catch (Exception ex)
            {               
                response.IsSuccess = false;
                response.Message = ex.Message+Environment.NewLine+ msg;
                //App.NotificationContainer.AddNotifiaction(new NotificationItem("更新失败", response.Message, NotifiactionType.Error));
            }
            return response;
        }
        public async Task<Response> PostAsync<T>(string uri, T ins)
        {
            string full_uri = BaseUri + uri;
            Response response = new();

            var json = JsonSerializer.Serialize(ins, jsonSerializerOptions);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            string msg = string.Empty;
            try
            {
                var responseM = await _Client.PostAsync(full_uri, content);
                msg = await responseM.Content.ReadAsStringAsync();
                responseM.EnsureSuccessStatusCode();

                string responseBody = await responseM.Content.ReadAsStringAsync();
                response = JsonSerializer.Deserialize<Response>(responseBody);
            }
            catch (Exception ex)
            {
                response.success = false;
                response.msg = ex.Message+Environment.NewLine+msg;

                //App.NotificationContainer.AddNotifiaction(new NotificationItem("添加失败", response.Message, NotifiactionType.Error));
            }
            return response;
        }
        public async Task<Response<T1>> PostAsync<T,T1>(string uri, T ins)
        {
            string full_uri = BaseUri + uri;
            Response<T1> response = new();

            var json = JsonSerializer.Serialize(ins, jsonSerializerOptions);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            string msg = string.Empty;
            try
            {
                var responseM = await _Client.PostAsync(full_uri, content);
                msg = await responseM.Content.ReadAsStringAsync();
                responseM.EnsureSuccessStatusCode();

                string responseBody = await responseM.Content.ReadAsStringAsync();
                response.Ins = JsonSerializer.Deserialize<T1>(responseBody);
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message = ex.Message + Environment.NewLine + msg;

                //App.NotificationContainer.AddNotifiaction(new NotificationItem("添加失败", response.Message, NotifiactionType.Error));
            }
            return response;
        }
    }

调用方式:

 var responseResult = await _httpService.PostAsync("AccelerationFault", accelerationMessage);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值