-
Notifications
You must be signed in to change notification settings - Fork 50
/
RestApiVisitHelper.cs
192 lines (169 loc) · 5.51 KB
/
RestApiVisitHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
public class RestApiVisitHelper
{
// 鉴权 token 的请求头属性名称
public String TokenHeaderName { get; set; }
// 默认的鉴权 token 信息
public String DefaultToken { get; set; }
public RestApiVisitHelper()
{
}
// 构造时设置鉴权 token 的请求头属性名称
public RestApiVisitHelper(String tokenHeaderName)
{
TokenHeaderName = tokenHeaderName;
}
// 构造时设置鉴权 token 的请求头属性名称,以及默认的 token 值
public RestApiVisitHelper(String tokenHeaderName, String defaultToken){
TokenHeaderName = tokenHeaderName;
DefaultToken = defaultToken;
}
public String Get(string apiUrl, Hashtable queryParams)
{
return Get(apiUrl, queryParams, DefaultToken);
}
private String Get(string apiUrl, Hashtable queryParams, String token)
{
System.Net.WebClient webClientObj = CreateWebClient(token);
apiUrl = FormatUrl(apiUrl, queryParams);
try
{
String result = webClientObj.DownloadString(apiUrl);
return result;
}
catch (Exception ce)
{
return WhenError(ce);
}
}
/// <summary>
/// Post Api 返回结果文本,使用默认的鉴权 token
/// </summary>
/// <param name="apiUrl"></param>
/// <param name="queryParams"></param>
/// <param name="body"></param>
/// <returns></returns>
public String Post(string apiUrl, Hashtable queryParams, JObject body)
{
return Post(apiUrl, queryParams, body, DefaultToken);
}
/// <summary>
/// Post Api 返回结果文本
/// </summary>
/// <param name="apiUrl"></param>
/// <param name="queryParams"></param>
/// <param name="body"></param>
/// <param name="token"></param>
/// <returns></returns>
public String Post(string apiUrl, Hashtable queryParams, JObject body, String token)
{
System.Net.WebClient webClientObj = CreateWebClient(token);
apiUrl = FormatUrl(apiUrl, queryParams);
try
{
String result = webClientObj.UploadString(apiUrl, "POST", body.ToString(Newtonsoft.Json.Formatting.None));
return result;
}
catch (Exception ce)
{
return WhenError(ce);
}
}
public String Put(string apiUrl, Hashtable queryParams, JObject body)
{
return Put(apiUrl, queryParams, body, DefaultToken);
}
public String Put(string apiUrl, Hashtable queryParams, JObject body, String token)
{
System.Net.WebClient webClientObj = CreateWebClient(token);
apiUrl = FormatUrl(apiUrl, queryParams);
try
{
String result = webClientObj.UploadString(apiUrl, "PUT", body.ToString(Newtonsoft.Json.Formatting.None));
return result;
}
catch (Exception ce)
{
return WhenError(ce);
}
}
private String _Delete(string apiUrl, Hashtable queryParams, String token)
{
System.Net.WebClient webClientObj = CreateWebClient(token);
apiUrl = FormatUrl(apiUrl, queryParams);
try
{
String result = webClientObj.UploadString(apiUrl, "DELETE", "");
return result;
}
catch (Exception ce)
{
return WhenError(ce);
}
}
#region 私有方法
// 创建 WebClient 并设置好 token 信息
private WebClient CreateWebClient(String token)
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
System.Net.WebClient webClientObj = new System.Net.WebClient();
webClientObj.Headers.Add("Accept", "application/json");
if (!String.IsNullOrEmpty(TokenHeaderName) && !String.IsNullOrEmpty(token))
{
webClientObj.Headers.Add(TokenHeaderName, token);
}
webClientObj.Encoding = Encoding.UTF8;
return webClientObj;
}
// 将查询参数格式化拼接到 url 上形成最终的访问地址
private String FormatUrl(String apiUrl, Hashtable queryParams)
{
if (queryParams == null) return apiUrl;
String queryString = "";
foreach (var k in queryParams.Keys)
{
if (!String.IsNullOrEmpty(queryString))
{
queryString += "&";
}
queryString += String.Format("{0}={1}", k, queryParams[k]);
}
if (!String.IsNullOrEmpty(queryString))
{
apiUrl += "?" + queryString;
}
return apiUrl;
}
// 异常时返回的信息:应该根据实际需要进行返回
private String WhenError(Exception e)
{
JObject result = new JObject();
result["err_code"] = -1;
if (e is WebException)
{
var we = (WebException)e;
if (we.Response != null) // 如果有输出则仍然返回实际输出
{
return new StreamReader(we.Response.GetResponseStream()).ReadToEnd();
}
else
{
result["err_msg"] = we.Message;
}
}
else
{
result["err_msg"] = e.Message;
}
return result.ToString(Newtonsoft.Json.Formatting.None);
}
#endregion
}