Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

189 wiersze
6.3 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. /// 支持环境 -.net4.0/-.net4.5
  13. /// 创建人:奉友福
  14. /// </summary>
  15. public class HttpRequestHelper
  16. {
  17. #region 私有变量
  18. #endregion
  19. #region 公用函数
  20. /// <summary>
  21. /// GET 同步请求
  22. /// 创建人:奉友福
  23. /// 创建时间:2020-11-19
  24. /// </summary>
  25. /// <param name="url">请求地址</param>
  26. /// <returns>超时时间设置,默认5秒</returns>
  27. public static string HttpGetRequest(string url, int _timeout = 2000)
  28. {
  29. string resultData = string.Empty;
  30. try
  31. {
  32. WebClient wc = new WebClient();
  33. byte[] bytes = wc.DownloadData(url);
  34. string s = Encoding.UTF8.GetString(bytes);
  35. return s;
  36. }
  37. catch (Exception e)
  38. {
  39. throw e;
  40. }
  41. return "";
  42. try
  43. {
  44. var getrequest = HttpRequest.GetInstance().CreateHttpRequest(url, "GET", _timeout);
  45. var getreponse = getrequest.GetResponse() as HttpWebResponse;
  46. resultData = HttpRequest.GetInstance().GetHttpResponse(getreponse, "GET");
  47. }
  48. catch (Exception)
  49. {
  50. throw;
  51. }
  52. return resultData;
  53. }
  54. /// <summary>
  55. /// POST 同步请求
  56. /// 创建人:奉友福
  57. /// 创建时间:2020-11-19
  58. /// </summary>
  59. /// <param name="url">请求地址</param>
  60. /// <param name="PostJsonData">请求数据</param>
  61. /// <returns></returns>
  62. public static string HttpPostRequest(string url, string PostJsonData, int _timeout = 2000)
  63. {
  64. string resultData = string.Empty;
  65. try
  66. {
  67. var postrequest = HttpRequest.GetInstance().CreateHttpRequest(url, "POST", _timeout, PostJsonData);
  68. var postreponse = postrequest.GetResponse() as HttpWebResponse;
  69. resultData = HttpRequest.GetInstance().GetHttpResponse(postreponse, "POST");
  70. }
  71. catch (Exception ex)
  72. {
  73. return ex.Message;
  74. }
  75. return resultData;
  76. }
  77. public static string HttpDeleteRequest(string url, string PostJsonData, int _timeout = 10000)
  78. {
  79. string resultData = string.Empty;
  80. try
  81. {
  82. var deleteRequest = HttpRequest.CreateDeleteHttpRequest(url, PostJsonData, _timeout);
  83. var deleteReponse = deleteRequest.GetResponse() as HttpWebResponse;
  84. using (StreamReader reader = new StreamReader(deleteReponse.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
  85. {
  86. resultData = reader.ReadToEnd();
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. }
  92. return resultData;
  93. }
  94. /// <summary>
  95. /// GET 同步请求
  96. /// </summary>
  97. /// <param name="url">地址</param>
  98. /// <param name="head">头</param>
  99. /// <param name="headInfo">内容</param>
  100. /// <returns></returns>
  101. public static string GetHttpGetResponseWithHead(string url, HttpRequestHeader head, string headInfo)
  102. {
  103. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  104. request.Method = "GET";
  105. request.ContentType = "application/json;charset=UTF-8";
  106. request.Timeout = 6000;
  107. request.Headers.Set(head, headInfo);
  108. StreamReader sr = null;
  109. HttpWebResponse response = null;
  110. Stream stream = null;
  111. try
  112. {
  113. response = (HttpWebResponse)request.GetResponse();
  114. stream = response.GetResponseStream();
  115. sr = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
  116. var resultData = sr.ReadToEnd();
  117. return resultData;
  118. }
  119. catch (Exception ex)
  120. {
  121. Console.WriteLine(url + " 访问失败:" + ex.Message);
  122. //return ex.Message;
  123. }
  124. finally
  125. {
  126. if (response != null)
  127. {
  128. response.Dispose();
  129. }
  130. if (stream != null)
  131. {
  132. stream.Dispose();
  133. }
  134. if (sr != null)
  135. {
  136. sr.Dispose();
  137. }
  138. }
  139. return null;
  140. }
  141. /// <summary>
  142. /// Post请求带Token
  143. /// 2021-2-2 by dulf
  144. /// </summary>
  145. /// <param name="url"></param>
  146. /// <param name="head"></param>
  147. /// <param name="headInfo"></param>
  148. /// <param name="postParam"></param>
  149. /// <returns></returns>
  150. public static string HttpPostResponseWithHead(string url, HttpRequestHeader head, string headInfo, string postParam, int Timeout = 6000)
  151. {
  152. string resultData = string.Empty;
  153. try
  154. {
  155. var postrequest = WebRequest.Create(url) as HttpWebRequest;
  156. postrequest.Timeout = Timeout;
  157. postrequest.Method = "POST";
  158. postrequest.ContentType = "application/json;charset=UTF-8";
  159. postrequest.Headers.Set(head, headInfo);
  160. byte[] data = Encoding.UTF8.GetBytes(postParam);
  161. using (Stream reqStream = postrequest.GetRequestStream())
  162. {
  163. reqStream.Write(data, 0, data.Length);
  164. var postreponse = postrequest.GetResponse() as HttpWebResponse;
  165. resultData = HttpRequest.GetInstance().GetHttpResponse(postreponse, "POST");
  166. reqStream.Close();
  167. }
  168. return resultData;
  169. }
  170. catch (Exception ex)
  171. {
  172. Console.Write("请求<HttpPostResponseWithHead>异常:" + ex.Message);
  173. }
  174. return "";
  175. }
  176. #endregion
  177. }
  178. }