25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

153 lines
5.4 KiB

  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 DataVAPI.Tool.API请求
  9. {
  10. /// <summary>
  11. /// HTTP请求类
  12. /// </summary>
  13. public class HttpRequest
  14. {
  15. #region 私有变量
  16. /// <summary>
  17. /// http请求超时时间设置
  18. /// 默认值:5秒
  19. /// </summary>
  20. private static int Timeout = 5000;
  21. #endregion
  22. #region 单例模式
  23. private static HttpRequest _HttpRequest = null;
  24. public static HttpRequest GetInstance()
  25. {
  26. if (_HttpRequest == null)
  27. {
  28. _HttpRequest = new HttpRequest();
  29. }
  30. return _HttpRequest;
  31. }
  32. private HttpRequest()
  33. {
  34. }
  35. #endregion
  36. #region 公用函数
  37. /// <summary>
  38. /// 函数名称:创建http请求
  39. /// 创建人:奉友福
  40. /// 创建时间:2020-11-19
  41. /// 例如GET 请求: 地址 + "GET"
  42. /// 例如POST请求: 地址 + "POST" + JSON
  43. /// </summary>
  44. /// <param name="url">http请求地址</param>
  45. /// <param name="requestType">http请求方式:GET/POST</param>
  46. /// <param name="strjson">http请求附带数据</param>
  47. /// <returns></returns>
  48. public HttpWebRequest CreateHttpRequest(string url, string requestType, int _timeout = 5000, params object[] strjson)
  49. {
  50. HttpWebRequest request = null;
  51. const string get = "GET";
  52. const string post = "POST";
  53. Timeout = _timeout;
  54. if (string.Equals(requestType, get, StringComparison.OrdinalIgnoreCase))
  55. {
  56. request = CreateGetHttpRequest(url);
  57. }
  58. if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase))
  59. {
  60. request = CreatePostHttpRequest(url, strjson[0].ToString());
  61. }
  62. return request;
  63. }
  64. /// <summary>
  65. /// http获取数据
  66. /// </summary>
  67. /// <param name="response"></param>
  68. /// <param name="requestType"></param>
  69. /// <returns></returns>
  70. public string GetHttpResponse(HttpWebResponse response, string requestType)
  71. {
  72. var resultData = string.Empty;
  73. const string post = "POST";
  74. string encoding = "UTF-8";
  75. if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase))
  76. {
  77. encoding = response.ContentEncoding;
  78. if (encoding == null || encoding.Length < 1)
  79. encoding = "UTF-8";
  80. }
  81. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)))
  82. {
  83. resultData = reader.ReadToEnd();
  84. }
  85. return resultData;
  86. }
  87. #endregion
  88. #region 私有函数
  89. /// <summary>
  90. /// http+GET请求
  91. /// </summary>
  92. /// <param name="url">请求地址</param>
  93. /// <returns>请求结果</returns>
  94. private static HttpWebRequest CreateGetHttpRequest(string url)
  95. {
  96. var getrequest = WebRequest.Create(url) as HttpWebRequest;
  97. getrequest.Method = "GET";
  98. getrequest.Timeout = Timeout;
  99. getrequest.ContentType = "application/json;charset=UTF-8";
  100. getrequest.Proxy = null;
  101. getrequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
  102. return getrequest;
  103. }
  104. /// <summary>
  105. /// http+POST请求
  106. /// </summary>
  107. /// <param name="url">请求地址</param>
  108. /// <param name="postData"></param>
  109. /// <returns>请求结果</returns>
  110. private static HttpWebRequest CreatePostHttpRequest(string url, string postData)
  111. {
  112. var postrequest = WebRequest.Create(url) as HttpWebRequest;
  113. //postrequest.KeepAlive = false;
  114. postrequest.Timeout = Timeout;
  115. postrequest.Method = "POST";
  116. postrequest.ContentType = "application/json;charset=UTF-8";
  117. //postrequest.ContentLength = postData.Length;
  118. //postrequest.AllowWriteStreamBuffering = false;
  119. //StreamWriter writer = new StreamWriter(postrequest.GetRequestStream(), Encoding.UTF8);
  120. //writer.Write(postData);
  121. //writer.Flush();
  122. byte[] data = Encoding.UTF8.GetBytes(postData);
  123. using (Stream reqStream = postrequest.GetRequestStream())
  124. {
  125. reqStream.Write(data, 0, data.Length);
  126. reqStream.Close();
  127. }
  128. return postrequest;
  129. }
  130. public static HttpWebRequest CreateDeleteHttpRequest(string url, string postJson, int _timeout = 5000)
  131. {
  132. var deleteRequest = WebRequest.Create(url) as HttpWebRequest;
  133. deleteRequest.Timeout = _timeout;
  134. deleteRequest.Method = "DELETE";
  135. deleteRequest.ContentType = "application/json;charset=UTF-8";
  136. byte[] data = Encoding.UTF8.GetBytes(postJson);
  137. using (Stream reqStream = deleteRequest.GetRequestStream())
  138. {
  139. reqStream.Write(data, 0, data.Length);
  140. reqStream.Close();
  141. }
  142. return deleteRequest;
  143. }
  144. #endregion
  145. }
  146. }