HttpClient RestFul InterFace

本文介绍了一个基于.NET的HTTP客户端服务实现,包括GET、POST和PUT请求的处理方式,并使用了Json.NET进行序列化和反序列化操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

namespace Collinson.Http.Security
{
    public interface IJsonClientService
    {
        T Get<T>(string uri);
        T Get<T>(string uri, object data);
        T Post<T>(string uri);
        T Post<T>(string uri, object data);
        T Put<T>(string uri);
        T Put<T>(string uri, object data);
    }
}



using Newtonsoft.Json;

using System;
using System.Net;
using System.Net.Http;
using System.Text;
namespace Collinson.Http.Security
{
    public class JsonClientService : IJsonClientService
    {
        private readonly HttpClient client = null;
        public JsonClientService(ClientSettings settings)
        {
            this.client = SigningHttpClientFactory.Create(settings.Username, settings.Password);
        }
        public T Get<T>(string uri)
        {
            HttpResponseMessage result = this.client.GetAsync(uri).Result;
            this.ThrowIfUnauthorized(result);
            string result2 = result.Content.ReadAsStringAsync().Result;
            return JsonConvert.DeserializeObject<T>(result2);
        }
        public T Get<T>(string uri, object data)
        {
            string requestUri = string.Format("{0}?{1}", uri, HttpExtensions.ToQueryString(data));
            HttpResponseMessage result = this.client.GetAsync(requestUri).Result;
            this.ThrowIfUnauthorized(result);
            string result2 = result.Content.ReadAsStringAsync().Result;
            return JsonConvert.DeserializeObject<T>(result2);
        }
        public T Post<T>(string uri)
        {
            HttpResponseMessage result = this.client.PostAsync(uri, new StringContent(string.Empty)).Result;
            this.ThrowIfUnauthorized(result);
            string result2 = result.Content.ReadAsStringAsync().Result;
            return JsonConvert.DeserializeObject<T>(result2);
        }
        public T Post<T>(string uri, object data)
        {
            string content = JsonConvert.SerializeObject(data);
            HttpResponseMessage result = this.client.PostAsync(uri, new StringContent(content, Encoding.UTF8, "application/json")).Result;
            this.ThrowIfUnauthorized(result);
            string result2 = result.Content.ReadAsStringAsync().Result;
            return JsonConvert.DeserializeObject<T>(result2);
        }
        public T Put<T>(string uri)
        {
            HttpResponseMessage result = this.client.PutAsync(uri, new StringContent(string.Empty)).Result;
            this.ThrowIfUnauthorized(result);
            string result2 = result.Content.ReadAsStringAsync().Result;
            return JsonConvert.DeserializeObject<T>(result2);
        }
        public T Put<T>(string uri, object data)
        {
            string content = JsonConvert.SerializeObject(data);
            HttpResponseMessage result = this.client.PutAsync(uri, new StringContent(content, Encoding.UTF8, "application/json")).Result;
            this.ThrowIfUnauthorized(result);
            string result2 = result.Content.ReadAsStringAsync().Result;
            return JsonConvert.DeserializeObject<T>(result2);
        }
        private void ThrowIfUnauthorized(HttpResponseMessage response)
        {
            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new UnauthorizedAccessException("You are not allowed to access the resource");
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值