RestClient.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SIMDP.Project
  9. {
  10. /// <summary>
  11. /// 请求类型
  12. /// </summary>
  13. public enum EnumHttpVerb
  14. {
  15. GET,
  16. POST,
  17. PUT,
  18. DELETE
  19. }
  20. public class RestClient
  21. {
  22. #region 属性
  23. /// <summary>
  24. /// 端点路径
  25. /// </summary>
  26. public string EndPoint { get; set; }
  27. /// <summary>
  28. /// 请求方式
  29. /// </summary>
  30. public EnumHttpVerb Method { get; set; }
  31. /// <summary>
  32. /// 文本类型(1、application/json 2、txt/html)
  33. /// </summary>
  34. public string ContentType { get; set; }
  35. /// <summary>
  36. /// 请求的数据(一般为JSon格式)
  37. /// </summary>
  38. public string PostData { get; set; }
  39. #endregion
  40. #region 初始化
  41. public RestClient()
  42. {
  43. EndPoint = "";
  44. Method = EnumHttpVerb.GET;
  45. ContentType = "application/json";
  46. PostData = "";
  47. }
  48. public RestClient(string endpoint)
  49. {
  50. EndPoint = endpoint;
  51. Method = EnumHttpVerb.GET;
  52. ContentType = "application/json";
  53. PostData = "";
  54. }
  55. public RestClient(string endpoint, EnumHttpVerb method)
  56. {
  57. EndPoint = endpoint;
  58. Method = method;
  59. ContentType = "application/json";
  60. PostData = "";
  61. }
  62. public RestClient(string endpoint, EnumHttpVerb method, string postData)
  63. {
  64. EndPoint = endpoint;
  65. Method = method;
  66. ContentType = "application/json";
  67. PostData = postData;
  68. }
  69. #endregion
  70. #region 方法
  71. /// <summary>
  72. /// http请求(不带参数请求)
  73. /// </summary>
  74. /// <returns></returns>
  75. public string HttpRequest()
  76. {
  77. return HttpRequest("");
  78. }
  79. /// <summary>
  80. /// http请求(带参数)
  81. /// </summary>
  82. /// <param name="parameters">parameters例如:?name=LiLei</param>
  83. /// <returns></returns>
  84. public string HttpRequest(string parameters)
  85. {
  86. var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
  87. request.Method = Method.ToString();
  88. request.ContentLength = 0;
  89. request.ContentType = ContentType;
  90. if (!string.IsNullOrEmpty(PostData) && Method == EnumHttpVerb.POST)
  91. {
  92. var bytes = Encoding.UTF8.GetBytes(PostData);
  93. request.ContentLength = bytes.Length;
  94. using (var writeStream = request.GetRequestStream())
  95. {
  96. writeStream.Write(bytes, 0, bytes.Length);
  97. }
  98. }
  99. using (var response = (HttpWebResponse)request.GetResponse())
  100. {
  101. var responseValue = string.Empty;
  102. if (response.StatusCode != HttpStatusCode.OK)
  103. {
  104. var message = string.Format("请求数据失败. 返回的 HTTP 状态码:{0}", response.StatusCode);
  105. throw new ApplicationException(message);
  106. }
  107. using (var responseStream = response.GetResponseStream())
  108. {
  109. if (responseStream != null)
  110. using (var reader = new StreamReader(responseStream))
  111. {
  112. responseValue = reader.ReadToEnd();
  113. }
  114. }
  115. return responseValue;
  116. }
  117. }
  118. #endregion
  119. }
  120. }