大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
1 usingRestSharp;2 usingRestSharp.Authenticators;3 usingSystem;4
5 namespaceHB.Common.Tools.Http6 {7 ///
8 ///Rest接口执行者9 ///
10 public classRestSharpClient : IRestSharp11 {12 ///
13 ///请求客户端14 ///
15 privateRestClient client;16
17 ///
18 ///接口基地址 格式:http://www.xxx.com/
19 ///
20 private string BaseUrl { get; set; }21
22 ///
23 ///默认的时间参数格式24 ///
25 private string DefaultDateParameterFormat { get; set; }26
27 ///
28 ///默认验证器29 ///
30 private IAuthenticator DefaultAuthenticator { get; set; }31
32 ///
33 ///构造函数34 ///
35 ///
36 ///
37 public RestSharpClient(string baseUrl, IAuthenticator authenticator = null)38 {39 BaseUrl =baseUrl;40 client = newRestClient(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 ///
54 ///通用执行方法55 ///
56 /// 请求参数
57 ///
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 ///
63 ///
64 publicIRestResponse Execute(IRestRequest request)65 {66 request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ?DefaultDateParameterFormat : request.DateFormat;67 var response =client.Execute(request);68 returnresponse;69 }70
71 ///
72 ///同步执行方法73 ///
74 /// 返回的泛型对象
75 /// 请求参数
76 ///
77 ///var client = new RestSharpClient(“http://localhost:82/”);78 ///var result = client.Execute>(new RestRequest(“api/values”, Method.GET));79 ///
80 ///
81 public T Execute(IRestRequest request) where T : new()82 {83 request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ?DefaultDateParameterFormat : request.DateFormat;84 var response = client.Execute(request);85 returnresponse.Data;86 }87
88 ///
89 ///异步执行方法90 ///
91 /// 请求参数
92 /// 回调函数
93 ///
94 ///调用实例:95 ///var client = new RestSharpClient(“http://localhost:62981/”);96 ///client.ExecuteAsync>(new RestRequest(“api/values”, Method.GET), result =>97 ///{98 ///var content = result.Content;//返回的字符串数据99 ///});100 ///
101 ///
102 public RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Actioncallback)103 {104 request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ?DefaultDateParameterFormat : request.DateFormat;105 returnclient.ExecuteAsync(request, callback);106 }107
108 ///
109 ///异步执行方法110 ///
111 /// 返回的泛型对象
112 /// 请求参数
113 /// 回调函数
114 ///
115 ///调用实例:116 ///var client = new RestSharpClient(“http://localhost:62981/”);117 ///client.ExecuteAsync>(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 ///
126 ///
127 public RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action> callback) where T : new()128 {129 request.DateFormat = string.IsNullOrEmpty(request.DateFormat) ?DefaultDateParameterFormat : request.DateFormat;130 return client.ExecuteAsync(request, callback);131 }132 }133 }
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/195896.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...