RestSharp使用总结

RestSharp是一个轻量的,不依赖任何第三方的组件或者类库的Http的组件。RestSharp具有以下的优点:
   1、支持.NET 3.5+,Silverlight 4, Windows Phone 7, Mono, MonoTouch, Mono for Android, Compact Framework 3.5等
   2、通过NuGet方便引入到任何项目 ( Install-Package restsharp )
   3、可以自动反序列化XML和JSON
   4、支持自定义的序列化与反序列化
   5、自动检测返回的内容类型
   6、支持HTTP的GET, POST, PUT, HEAD, OPTIONS, DELETE等操作
   7、可以上传多文件
   8、支持oAuth 1, oAuth 2, Basic, NTLM and Parameter-based Authenticators等授权验证等
   9、支持异步操作
  10、极易上手并应用到任何项目中

下面则是本人对RestSharp的一些整理和扩展:

 1 using RestSharp;
 2 using System;
 3 
 4 namespace HB.Common.Tools.Http
 5 {
 6     /// <summary>
 7     /// API请求执行者接口
 8     /// </summary>
 9     public interface IRestSharp
10     {
11         /// <summary>
12         /// 同步执行方法
13         /// </summary>
14         /// <param name="request"></param>
15         /// <returns></returns>
16         IRestResponse Execute(IRestRequest request);
17 
18         /// <summary>
19         /// 同步执行方法
20         /// </summary>
21         /// <typeparam name="T">返回值</typeparam>
22         /// <param name="request">请求参数</param>
23         /// <returns></returns>
24         T Execute<T>(IRestRequest request) where T : new();
25 
26         /// <summary>
27         /// 异步执行方法
28         /// </summary>
29         /// <param name="request">请求参数</param>
30         /// <param name="callback"></param>
31         /// <returns></returns>
32         RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<IRestResponse> callback);
33 
34         /// <summary>
35         /// 异步执行方法
36         /// </summary>
37         /// <typeparam name="T"></typeparam>
38         /// <param name="request"></param>
39         /// <param name="callback"></param>
40         /// <returns></returns>
41         RestRequestAsyncHandle ExecuteAsync<T>(IRestRequest request, Action<IRestResponse<T>> callback) where T : new();
42     }
43 }
IRestSharp.cs
  1 using RestSharp;
  2 using RestSharp.Authenticators;
  3 using System;
  4 
  5 namespace HB.Common.Tools.Http
  6 {
  7     /// <summary>
  8     /// Rest接口执行者
  9     /// </summary>
 10     public class RestSharpClient : IRestSharp
 11     {
 12         /// <summary>
 13         /// 请求客户端
 14         /// </summary>
 15         private RestClient client;
 16 
 17         /// <summary>
 18         /// 接口基地址 格式:http://www.xxx.com/
 19         /// </summary>
 20         private string BaseUrl { get; set; }
 21 
 22         /// <summary>
 23         /// 默认的时间参数格式
 24         /// </summary>
 25         private string DefaultDateParameterFormat { get; set; }
 26 
 27         /// <summary>
 28         /// 默认验证器
 29         /// </summary>
 30         private IAuthenticator DefaultAuthenticator { get; set; }
 31 
 32         /// <summary>
 33         /// 构造函数
 34         /// </summary>
 35         /// <param name="baseUrl"></param>
 36         /// <param name="authenticator"></param>
 37         public RestSharpClient(string baseUrl, IAuthenticator authenticator = null)
 38         {
 39             BaseUrl = baseUrl;
 40             client = new RestClient(BaseUrl);
 41             DefaultAuthenticator = authenticator;
 42 
 43             //默认时间显示格式
 44             DefaultDateParameterFormat = "yyyy-MM-dd HH:mm:ss";
 45 
 46             //默认校验器
 47             if (DefaultAuthenticator != null)
 48             {
 49                 client.Authenticator = DefaultAuthenticator;
 50             }
 51         }
 52 
 53         /// <summary>
 54         /// 通用执行方法
 55         /// </summary>
 56         /// <param name="request">请求参数</param>
 57         /// <remarks>
 58         /// 调用实例:
 59         /// var client = new RestSharpClient("http://localhost:82/");
 60         /// var result = client.Execute(new RestRequest("api/values", Method.GET));
 61         /// var content = result.Content;//返回的字符串数据
 62         /// </remarks>
 63         /// <returns></returns>
 64         public IRestResponse Execute(IRestRequest request)
 65         {
 66             request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat;
 67             var response = client.Execute(request);
 68             return response;
 69         }
 70 
 71         /// <summary>
 72         /// 同步执行方法
 73         /// </summary>
 74         /// <typeparam name="T">返回的泛型对象</typeparam>
 75         /// <param name="request">请求参数</param>
 76         /// <remarks>
 77         ///  var client = new RestSharpClient("http://localhost:82/");
 78         ///  var result = client.Execute<List<string>>(new RestRequest("api/values", Method.GET)); 
 79         /// </remarks>
 80         /// <returns></returns>
 81         public T Execute<T>(IRestRequest request) where T : new()
 82         {
 83             request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat;
 84             var response = client.Execute<T>(request);
 85             return response.Data;
 86         }
 87 
 88         /// <summary>
 89         /// 异步执行方法
 90         /// </summary>
 91         /// <param name="request">请求参数</param>
 92         /// <param name="callback">回调函数</param>
 93         /// <remarks>
 94         /// 调用实例:
 95         /// var client = new RestSharpClient("http://localhost:62981/");
 96         /// client.ExecuteAsync<List<string>>(new RestRequest("api/values", Method.GET), result =>
 97         /// {
 98         ///      var content = result.Content;//返回的字符串数据
 99         /// });
100         /// </remarks>
101         /// <returns></returns>
102         public RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<IRestResponse> callback)
103         {
104             request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat;
105             return client.ExecuteAsync(request, callback);
106         }
107 
108         /// <summary>
109         /// 异步执行方法
110         /// </summary>
111         /// <typeparam name="T">返回的泛型对象</typeparam>
112         /// <param name="request">请求参数</param>
113         /// <param name="callback">回调函数</param>
114         /// <remarks>
115         /// 调用实例:
116         /// var client = new RestSharpClient("http://localhost:62981/");
117         /// client.ExecuteAsync<List<string>>(new RestRequest("api/values", Method.GET), result =>
118         /// {
119         ///      if (result.StatusCode != HttpStatusCode.OK)
120         ///      {
121         ///         return;
122         ///      }
123         ///      var data = result.Data;//返回数据
124         /// });
125         /// </remarks>
126         /// <returns></returns>
127         public RestRequestAsyncHandle ExecuteAsync<T>(IRestRequest request, Action<IRestResponse<T>> callback) where T : new()
128         {
129             request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat;
130             return client.ExecuteAsync<T>(request, callback);
131         }
132     }
133 }
RestSharpClient.cs

 

posted @ 2017-11-13 23:27  ζ๓蜗牛  阅读(3867)  评论(0编辑  收藏  举报