标签:
简单封装RestClient
using RestSharp; using RestSharp.Deserializers; using RestSharp.Serializers; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Web; namespace RestSharp.Extension { public class RestAPIClient { private RestClient _client; public RestClient Client { get { return _client; } } public RestAPIClient(string baseUri) { this._client = new RestClient(baseUri); //使用CookieContainer自动管理cookie this._client.CookieContainer = new System.Net.CookieContainer(); } /// <summary> /// 发送一个HTTP请求 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="resource">资源地址</param> /// <param name="method">请求方式</param> /// <param name="body">请求参数</param> /// <param name="setRequest">设置请求参数委托</param> /// <returns>返回T对象</returns> /// <remarks>当Method为Get的时候,body只能是简单的匿名对象,即匿名对象中不能在包含匿名对象</remarks> public T Execute<T>(string resource, Method method = Method.POST, object body = null, Action<IRestRequest> setRequest = null) where T : new() { return ExecuteResponse<T>(resource, method, body, setRequest).Data; } /// <summary> /// 发送一个HTTP请求 /// </summary> /// <param name="resource">资源地址</param> /// <param name="method">请求方式</param> /// <param name="body">请求参数</param> /// <param name="setRequest">设置请求参数委托</param> /// <returns>返回一个包括所有服务器响应信息的原始对象</returns> public IRestResponse ExecuteResponse(string resource, Method method = Method.POST, object body = null, Action<IRestRequest> setRequest = null) { var request = BuildRequest(resource, method, body, setRequest); return _client.Execute(request); } /// <summary> /// 发送一个异步HTTP请求 /// </summary> /// <param name="resource">资源地址</param> /// <param name="callback">异步回调函数</param> /// <param name="method">请求方式</param> /// <param name="body">请求参数</param> /// <param name="setRequest">设置请求参数委托</param> /// <returns></returns> public RestRequestAsyncHandle ExecuteResponseAsync(string resource, Action<IRestResponse> callback, Method method = Method.POST, object body = null, Action<IRestRequest> setRequest = null) { var request = BuildRequest(resource, method, body, setRequest); return _client.ExecuteAsync(request, callback); } /// <summary> /// 发送一个HTTP请求 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="resource">资源地址</param> /// <param name="method">请求方式</param> /// <param name="body">请求参数</param> /// <param name="setRequest">设置请求参数委托</param> /// <returns>返回一个包括所有服务器响应信息的原始对象及反序列化的T对象Data</returns> public IRestResponse<T> ExecuteResponse<T>(string resource, Method method = Method.POST, object body = null, Action<IRestRequest> setRequest = null) where T : new() { var request = BuildRequest(resource, method, body, setRequest); return _client.Execute<T>(request); } /// <summary> /// 发送一个异步HTTP请求 /// </summary> /// <param name="resource">资源地址</param> /// <param name="callback">回调函数</param> /// <param name="method">请求方式</param> /// <param name="body">请求参数</param> /// <param name="setRequest">设置请求参数委托</param> /// <returns></returns> public RestRequestAsyncHandle ExecuteResponseAsync<T>(string resource, Action<IRestResponse<T>> callback, Method method = Method.POST, object body = null, Action<IRestRequest> setRequest = null) where T : new() { var request = BuildRequest(resource, method, body, setRequest); return _client.ExecuteAsync<T>(request, callback); } /// <summary> /// 组装请求 /// </summary> /// <param name="resource">请求URL</param> /// <param name="method">请求类型</param> /// <param name="body">请求体</param> /// <param name="setRequest">设置请求参数委托</param> /// <returns></returns> private RestRequest BuildRequest(string resource, Method method = Method.GET, object body = null, Action<IRestRequest> setRequest = null) { if (string.IsNullOrWhiteSpace(resource)) { throw new ArgumentNullException("resource", "请求资源url不能为空。"); } var request = new RestRequest(method) { Resource = resource, RequestFormat = DataFormat.Json, DateFormat = "yyyy-MM-dd HH:mm:ss", //JsonSerializer = new RestJsonSerializer("application/json") }; //默认传递数据格式及响应格式都为json request.AddHeader("Content-Type", "application/json"); request.AddHeader("Accept", "application/json"); //添加请求体 if (body != null && new Method[] { Method.PUT, Method.POST }.Contains(method)) { //该方法只能用于POST或PUT请求 request.AddBody(body); } else { //Get或其他请求,body只能是一个简单匿名对象 request.AddObject(body); } //执行设置请求参数委托 if (setRequest != null) { setRequest.Invoke(request); } return request; } } }
客户端
using RestSharp; using RestSharp.Extension; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using Test.Models.Request; namespace Test { class Program { static void Main(string[] args) { string baseUri = "http://localhost:3677/"; RestAPIClient client = new RestAPIClient(baseUri); var getData = new { Name = "君主论", Price = 13 }; var postData = new { Name = "333", user = new { Id = "123456", Name = "王小丫", Age = 13, books = new[] { new { Name="平凡的世界",Price=22 }, new { Name="君主论",Price=13 } } } }; //发送一个GET请求 var getResponse = client.ExecuteResponse<GetResponse>("Hello/TestGet", Method.GET, getData); //POST请求 var postResponse = client.ExecuteResponse<PostResponse>("Hello/TestPost", Method.POST, postData); //异步POST请求 client.ExecuteResponseAsync<PostResponse>("Hello/TestPost", response => { Console.WriteLine(response.Content); Console.WriteLine(response.Data.Name); }, Method.POST, postData); Console.ReadKey(); } } }
标签:
原文地址:http://www.cnblogs.com/Jabben/p/5744477.html