Http.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Web;
  11. namespace Update.Utils
  12. {
  13. public static class Http
  14. {
  15. public struct Response
  16. {
  17. public HttpStatusCode code;
  18. public HttpContent message;
  19. public string url;
  20. }
  21. public struct Request
  22. {
  23. /// <summary>
  24. /// 请求的网址
  25. /// </summary>
  26. public string uri;
  27. /// <summary>
  28. /// 头信息
  29. /// </summary>
  30. public Dictionary<string, object> headers;
  31. /// <summary>
  32. /// 请求的数据
  33. /// </summary>
  34. public Dictionary<string, object> data;
  35. /// <summary>
  36. /// 上传的文件
  37. /// </summary>
  38. public Dictionary<string, string> files;
  39. /// <summary>
  40. /// 上传的文件二进制流
  41. /// </summary>
  42. public Dictionary<string, byte[]> fileStream;
  43. /// <summary>
  44. /// 请求类型
  45. /// </summary>
  46. public RequestContentType contentType;
  47. /// <summary>
  48. /// 请求方式
  49. /// </summary>
  50. public Method method;
  51. public void Init()
  52. {
  53. uri = "";
  54. headers = new Dictionary<string, object>();
  55. data = new Dictionary<string, object>();
  56. files = new Dictionary<string, string>();
  57. fileStream = new Dictionary<string, byte[]>();
  58. contentType = RequestContentType.Default;
  59. method = Method.Get;
  60. }
  61. public Request(string uri, Method method)
  62. {
  63. this.uri = uri;
  64. this.method = method;
  65. this.headers = new Dictionary<string, object>();
  66. this.data = null;
  67. files = null;
  68. fileStream = null;
  69. contentType = RequestContentType.Default;
  70. }
  71. public Request(string uri, Method method, Dictionary<string, object> data)
  72. {
  73. this.uri = uri;
  74. this.method = method;
  75. this.headers = new Dictionary<string, object>();
  76. this.data = data;
  77. files = null;
  78. fileStream = null;
  79. contentType = RequestContentType.Default;
  80. }
  81. public Request(string uri, Method method, Dictionary<string, object> headers, Dictionary<string, object> data)
  82. {
  83. this.uri = uri;
  84. this.method = method;
  85. this.headers = headers;
  86. this.data = data;
  87. files = null;
  88. fileStream = null;
  89. contentType = RequestContentType.Default;
  90. }
  91. public Request(string uri, Method method, RequestContentType contentType, Dictionary<string, object> headers, Dictionary<string, object> data)
  92. {
  93. this.uri = uri;
  94. this.method = method;
  95. this.headers = headers;
  96. this.data = data;
  97. files = null;
  98. fileStream = null;
  99. this.contentType = contentType;
  100. }
  101. public Request(string uri, Method method, Dictionary<string, object> headers, Dictionary<string, object> data, Dictionary<string, string> files)
  102. {
  103. this.uri = uri;
  104. this.method = method;
  105. this.headers = headers;
  106. this.data = data;
  107. this.files = files;
  108. fileStream = null;
  109. contentType = RequestContentType.Default;
  110. }
  111. public Request(string uri, Method method, Dictionary<string, object> headers, Dictionary<string, object> data, Dictionary<string, string> files, Dictionary<string, byte[]> fileStream)
  112. {
  113. this.uri = uri;
  114. this.method = method;
  115. this.headers = headers;
  116. this.data = data;
  117. this.files = files;
  118. this.fileStream = fileStream;
  119. contentType = RequestContentType.Default;
  120. }
  121. }
  122. /// <summary>
  123. /// 请求内容类型
  124. /// </summary>
  125. public enum RequestContentType
  126. {
  127. Default, Json
  128. }
  129. /// <summary>
  130. /// 请求方法
  131. /// </summary>
  132. public enum Method
  133. {
  134. Get, Post, Put, Delete, Patch
  135. }
  136. /// <summary>
  137. /// 初始化请求头
  138. /// </summary>
  139. /// <param name="httpClient"></param>
  140. /// <param name="headers"></param>
  141. private static void InitClientHeaders(ref HttpClient httpClient, Dictionary<string, object> headers)
  142. {
  143. if (headers != null)
  144. {
  145. foreach (var header in headers)
  146. {
  147. if (!string.IsNullOrEmpty(header.Key) && !string.IsNullOrEmpty(header.Value.ToString()))
  148. {
  149. httpClient.DefaultRequestHeaders.Add(header.Key, header.Value.ToString());
  150. }
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// 获取
  156. /// </summary>
  157. /// <param name="request"></param>
  158. /// <returns></returns>
  159. public static async Task<HttpResponseMessage> Fetch(Request request)
  160. {
  161. HttpResponseMessage response;
  162. switch (request.method)
  163. {
  164. case Method.Post:
  165. response = await Post(request);
  166. break;
  167. case Method.Put:
  168. response = await Put(request);
  169. break;
  170. case Method.Patch:
  171. response = await Patch(request);
  172. break;
  173. case Method.Delete:
  174. response = await Delete(request);
  175. break;
  176. default:
  177. response = await Get(request);
  178. break;
  179. }
  180. return response;
  181. }
  182. public static async Task<HttpResponseMessage> Get(Request request)
  183. {
  184. HttpResponseMessage response = null;
  185. if (!string.IsNullOrEmpty(request.uri))
  186. {
  187. HttpClient client = new HttpClient();
  188. string url = request.uri;
  189. InitClientHeaders(ref client, request.headers);
  190. if (request.data != null)
  191. {
  192. //client.DefaultRequestHeaders.Add()
  193. url = string.Concat(url, "?");
  194. foreach (var item in request.data)
  195. {
  196. url = string.Concat(url, $"{item.Key}={HttpUtility.UrlEncode(item.Value.ToString())}&");
  197. }
  198. url = url.Substring(0, url.Length - 1);
  199. }
  200. try
  201. {
  202. response = await client.GetAsync(url);
  203. }
  204. catch (Exception ex)
  205. {
  206. Log.Error(ex);
  207. }
  208. finally
  209. {
  210. client?.Dispose();
  211. }
  212. }
  213. return response;
  214. }
  215. private static HttpContent InitNonGetContent(Request request)
  216. {
  217. HttpContent content = null;
  218. if (request.contentType == RequestContentType.Json)
  219. {
  220. // 发送json 数据
  221. HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(request.data), Encoding.UTF8);
  222. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  223. content = httpContent;
  224. }
  225. else
  226. //if (request.files != null || request.fileStream != null)
  227. {
  228. // 普通的键值对提交
  229. MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent();
  230. //FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent();
  231. if (request.files != null)
  232. {
  233. foreach (var file in request.files)
  234. {
  235. multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(file.Value)), file.Key, Path.GetFileName(file.Value));
  236. }
  237. }
  238. if (request.fileStream != null)
  239. {
  240. foreach (var file in request.fileStream)
  241. {
  242. multipartFormDataContent.Add(new ByteArrayContent(file.Value), file.Key, file.Key);
  243. }
  244. }
  245. if (request.data != null)
  246. {
  247. foreach (var d in request.data)
  248. {
  249. multipartFormDataContent.Add(new StringContent(d.Value.ToString(), Encoding.UTF8), d.Key);
  250. }
  251. }
  252. content = multipartFormDataContent;
  253. }
  254. return content;
  255. }
  256. public static async Task<HttpResponseMessage> Post(Request request)
  257. {
  258. HttpResponseMessage response = null;
  259. if (!string.IsNullOrEmpty(request.uri))
  260. {
  261. HttpClient client = new HttpClient();
  262. HttpContent content = InitNonGetContent(request);
  263. try
  264. {
  265. InitClientHeaders(ref client, request.headers);
  266. response = await client.PostAsync(request.uri, content);
  267. //client.Dispose();
  268. }
  269. catch (Exception ex)
  270. {
  271. Log.Error(ex);
  272. }
  273. finally
  274. {
  275. client?.Dispose();
  276. }
  277. }
  278. return response;
  279. }
  280. public static async Task<HttpResponseMessage> Patch(Request request)
  281. {
  282. HttpResponseMessage response = null;
  283. if (!string.IsNullOrEmpty(request.uri))
  284. {
  285. HttpClient client = new HttpClient();
  286. HttpContent content = InitNonGetContent(request);
  287. HttpRequestMessage httpRequestMessage = new HttpRequestMessage(new HttpMethod("PATCH"), request.uri);
  288. try
  289. {
  290. InitClientHeaders(ref client, request.headers);
  291. httpRequestMessage.Content = content;
  292. response = await client.SendAsync(httpRequestMessage);
  293. //client.Dispose();
  294. }
  295. catch (Exception ex)
  296. {
  297. Log.Error(ex);
  298. }
  299. finally
  300. {
  301. client?.Dispose();
  302. }
  303. httpRequestMessage.Dispose();
  304. }
  305. return response;
  306. }
  307. public static async Task<HttpResponseMessage> Put(Request request)
  308. {
  309. HttpResponseMessage response = null;
  310. if (!string.IsNullOrEmpty(request.uri))
  311. {
  312. HttpClient client = new HttpClient();
  313. HttpContent content = InitNonGetContent(request);
  314. try
  315. {
  316. InitClientHeaders(ref client, request.headers);
  317. response = await client.PutAsync(request.uri, content);
  318. //client.Dispose();
  319. }
  320. catch (Exception ex)
  321. {
  322. Log.Error(ex);
  323. }
  324. finally
  325. {
  326. client?.Dispose();
  327. }
  328. }
  329. return response;
  330. }
  331. public static async Task<HttpResponseMessage> Delete(Request request)
  332. {
  333. HttpResponseMessage response = null;
  334. if (!string.IsNullOrEmpty(request.uri))
  335. {
  336. HttpClient client = new HttpClient();
  337. string url = request.uri;
  338. try
  339. {
  340. InitClientHeaders(ref client, request.headers);
  341. if (request.data != null)
  342. {
  343. url = string.Concat(url, "?");
  344. foreach (var item in request.data)
  345. {
  346. url = string.Concat(url, $"{item.Key}={HttpUtility.UrlEncode(item.Value.ToString())}&");
  347. }
  348. }
  349. response = await client.DeleteAsync(request.uri);
  350. //client.Dispose();
  351. }
  352. catch (Exception ex)
  353. {
  354. Log.Error(ex);
  355. }
  356. finally
  357. {
  358. client?.Dispose();
  359. }
  360. }
  361. return response;
  362. }
  363. }
  364. }