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的一些整理和扩展:
using RestSharp;
using System; namespace HB.Common.Tools.Http
{
/// <summary>
/// API请求执行者接口
/// </summary>
public interface IRestSharp
{
/// <summary>
/// 同步执行方法
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
IRestResponse Execute(IRestRequest request); /// <summary>
/// 同步执行方法
/// </summary>
/// <typeparam name="T">返回值</typeparam>
/// <param name="request">请求参数</param>
/// <returns></returns>
T Execute<T>(IRestRequest request) where T : new(); /// <summary>
/// 异步执行方法
/// </summary>
/// <param name="request">请求参数</param>
/// <param name="callback"></param>
/// <returns></returns>
RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<IRestResponse> callback); /// <summary>
/// 异步执行方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="request"></param>
/// <param name="callback"></param>
/// <returns></returns>
RestRequestAsyncHandle ExecuteAsync<T>(IRestRequest request, Action<IRestResponse<T>> callback) where T : new();
}
}
IRestSharp.cs
using RestSharp;
using RestSharp.Authenticators;
using System; namespace HB.Common.Tools.Http
{
/// <summary>
/// Rest接口执行者
/// </summary>
public class RestSharpClient : IRestSharp
{
/// <summary>
/// 请求客户端
/// </summary>
private RestClient client; /// <summary>
/// 接口基地址 格式:http://www.xxx.com/
/// </summary>
private string BaseUrl { get; set; } /// <summary>
/// 默认的时间参数格式
/// </summary>
private string DefaultDateParameterFormat { get; set; } /// <summary>
/// 默认验证器
/// </summary>
private IAuthenticator DefaultAuthenticator { get; set; } /// <summary>
/// 构造函数
/// </summary>
/// <param name="baseUrl"></param>
/// <param name="authenticator"></param>
public RestSharpClient(string baseUrl, IAuthenticator authenticator = null)
{
BaseUrl = baseUrl;
client = new RestClient(BaseUrl);
DefaultAuthenticator = authenticator; //默认时间显示格式
DefaultDateParameterFormat = "yyyy-MM-dd HH:mm:ss"; //默认校验器
if (DefaultAuthenticator != null)
{
client.Authenticator = DefaultAuthenticator;
}
} /// <summary>
/// 通用执行方法
/// </summary>
/// <param name="request">请求参数</param>
/// <remarks>
/// 调用实例:
/// var client = new RestSharpClient("http://localhost:82/");
/// var result = client.Execute(new RestRequest("api/values", Method.GET));
/// var content = result.Content;//返回的字符串数据
/// </remarks>
/// <returns></returns>
public IRestResponse Execute(IRestRequest request)
{
request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat;
var response = client.Execute(request);
return response;
} /// <summary>
/// 同步执行方法
/// </summary>
/// <typeparam name="T">返回的泛型对象</typeparam>
/// <param name="request">请求参数</param>
/// <remarks>
/// var client = new RestSharpClient("http://localhost:82/");
/// var result = client.Execute<List<string>>(new RestRequest("api/values", Method.GET));
/// </remarks>
/// <returns></returns>
public T Execute<T>(IRestRequest request) where T : new()
{
request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat;
var response = client.Execute<T>(request);
return response.Data;
} /// <summary>
/// 异步执行方法
/// </summary>
/// <param name="request">请求参数</param>
/// <param name="callback">回调函数</param>
/// <remarks>
/// 调用实例:
/// var client = new RestSharpClient("http://localhost:62981/");
/// client.ExecuteAsync<List<string>>(new RestRequest("api/values", Method.GET), result =>
/// {
/// var content = result.Content;//返回的字符串数据
/// });
/// </remarks>
/// <returns></returns>
public RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action<IRestResponse> callback)
{
request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat;
return client.ExecuteAsync(request, callback);
} /// <summary>
/// 异步执行方法
/// </summary>
/// <typeparam name="T">返回的泛型对象</typeparam>
/// <param name="request">请求参数</param>
/// <param name="callback">回调函数</param>
/// <remarks>
/// 调用实例:
/// var client = new RestSharpClient("http://localhost:62981/");
/// client.ExecuteAsync<List<string>>(new RestRequest("api/values", Method.GET), result =>
/// {
/// if (result.StatusCode != HttpStatusCode.OK)
/// {
/// return;
/// }
/// var data = result.Data;//返回数据
/// });
/// </remarks>
/// <returns></returns>
public RestRequestAsyncHandle ExecuteAsync<T>(IRestRequest request, Action<IRestResponse<T>> callback) where T : new()
{
request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ? DefaultDateParameterFormat : request.DateFormat;
return client.ExecuteAsync<T>(request, callback);
}
}
}
RestSharpClient.cs