RestApi.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using RestSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  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. namespace ProjectManagementSystem.WebApiClient.Service
  11. {
  12. public class RestApi<T> where T : class, new()
  13. {
  14. public T Get(string url, object pars, object body, out string content)
  15. {
  16. IRestResponse<T> reval = GetApiInfo(url, pars, body, Method.GET);
  17. content = reval.Content;
  18. return reval.Data;
  19. }
  20. public T Post(string url, object pars, object body, out string content)
  21. {
  22. IRestResponse<T> reval = GetApiInfo(url, pars, body, Method.POST);
  23. content = reval.Content;
  24. return reval.Data;
  25. }
  26. public T Delete(string url, object pars, object body, out string content)
  27. {
  28. IRestResponse<T> reval = GetApiInfo(url, pars, body, Method.DELETE);
  29. content = reval.Content;
  30. return reval.Data;
  31. }
  32. public T Put(string url, object pars, object body, out string content)
  33. {
  34. IRestResponse<T> reval = GetApiInfo(url, pars, body, Method.PUT);
  35. content = reval.Content;
  36. return reval.Data;
  37. }
  38. public IRestResponse<T> GetApiInfo(string url, object pars, object body, Method type)
  39. {
  40. var request = new RestRequest(type);
  41. if (pars != null)
  42. {
  43. request.AddObject(pars);
  44. }
  45. if (body != null)
  46. {
  47. request.AddJsonBody(body);
  48. }
  49. var client = new RestClient(url);
  50. //client.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator("admin", "123456");
  51. //client.CookieContainer = new System.Net.CookieContainer();
  52. //client.ClearHandlers();
  53. //client.AddHandler("text/plain", () => new RestSharp.Serialization.Json.JsonDeserializer());
  54. IRestResponse<T> reval = client.Execute<T>(request);
  55. if (reval.ErrorException != null)
  56. {
  57. throw new Exception($"请求出错 {url} {reval.ErrorMessage} {reval.Content}");
  58. }
  59. return reval;
  60. }
  61. public string RequestUseHttpClient(string url, string data = "", Method requestMethod = Method.POST)
  62. {
  63. HttpClient http = new HttpClient(new HttpClientHandler()
  64. {
  65. //启用压缩,节省流量
  66. AutomaticDecompression = DecompressionMethods.GZip
  67. });
  68. HttpResponseMessage response = null;
  69. switch (requestMethod)
  70. {
  71. //case Method.:
  72. // HttpRequestMessage hrm = new HttpRequestMessage(HttpMethod.Get, url);
  73. // HttpContent hc = new StringContent(data);
  74. // hc.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
  75. // hrm.Content = hc;
  76. // response = http.SendAsync(hrm).Result;
  77. // break;
  78. case Method.GET:
  79. http.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded");
  80. response = http.GetAsync(url).Result;
  81. break;
  82. case Method.POST:
  83. HttpContent httpContent = new StringContent(data);
  84. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  85. response = http.PostAsync(url, httpContent).Result;
  86. break;
  87. case Method.PUT:
  88. HttpContent putContent = new StringContent(data);
  89. putContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  90. response = http.PutAsync(url, putContent).Result;
  91. break;
  92. default:
  93. break;
  94. }
  95. var res = response?.Content.ReadAsStringAsync().Result;
  96. return res;
  97. }
  98. }
  99. }