终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

329 righe
12 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 BPASmartClient.Helper
  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. /// <summary>
  179. /// HTTP请求类
  180. /// </summary>
  181. public class HttpRequest
  182. {
  183. #region 私有变量
  184. /// <summary>
  185. /// http请求超时时间设置
  186. /// 默认值:5秒
  187. /// </summary>
  188. private static int Timeout = 5000;
  189. #endregion
  190. #region 单例模式
  191. private static HttpRequest _HttpRequest = null;
  192. public static HttpRequest GetInstance()
  193. {
  194. if (_HttpRequest == null)
  195. {
  196. _HttpRequest = new HttpRequest();
  197. }
  198. return _HttpRequest;
  199. }
  200. private HttpRequest()
  201. {
  202. }
  203. #endregion
  204. #region 公用函数
  205. /// <summary>
  206. /// 函数名称:创建http请求
  207. /// 创建人:奉友福
  208. /// 创建时间:2020-11-19
  209. /// 例如GET 请求: 地址 + "GET"
  210. /// 例如POST请求: 地址 + "POST" + JSON
  211. /// </summary>
  212. /// <param name="url">http请求地址</param>
  213. /// <param name="requestType">http请求方式:GET/POST</param>
  214. /// <param name="strjson">http请求附带数据</param>
  215. /// <returns></returns>
  216. public HttpWebRequest CreateHttpRequest(string url, string requestType, int _timeout = 5000, params object[] strjson)
  217. {
  218. HttpWebRequest request = null;
  219. const string get = "GET";
  220. const string post = "POST";
  221. Timeout = _timeout;
  222. if (string.Equals(requestType, get, StringComparison.OrdinalIgnoreCase))
  223. {
  224. request = CreateGetHttpRequest(url);
  225. }
  226. if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase))
  227. {
  228. request = CreatePostHttpRequest(url, strjson[0].ToString());
  229. }
  230. return request;
  231. }
  232. /// <summary>
  233. /// http获取数据
  234. /// </summary>
  235. /// <param name="response"></param>
  236. /// <param name="requestType"></param>
  237. /// <returns></returns>
  238. public string GetHttpResponse(HttpWebResponse response, string requestType)
  239. {
  240. var resultData = string.Empty;
  241. const string post = "POST";
  242. string encoding = "UTF-8";
  243. if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase))
  244. {
  245. encoding = response.ContentEncoding;
  246. if (encoding == null || encoding.Length < 1)
  247. encoding = "UTF-8";
  248. }
  249. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)))
  250. {
  251. resultData = reader.ReadToEnd();
  252. }
  253. return resultData;
  254. }
  255. #endregion
  256. #region 私有函数
  257. /// <summary>
  258. /// http+GET请求
  259. /// </summary>
  260. /// <param name="url">请求地址</param>
  261. /// <returns>请求结果</returns>
  262. private static HttpWebRequest CreateGetHttpRequest(string url)
  263. {
  264. var getrequest = WebRequest.Create(url) as HttpWebRequest;
  265. getrequest.Method = "GET";
  266. getrequest.Timeout = Timeout;
  267. getrequest.ContentType = "application/json;charset=UTF-8";
  268. getrequest.Proxy = null;
  269. getrequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
  270. return getrequest;
  271. }
  272. /// <summary>
  273. /// http+POST请求
  274. /// </summary>
  275. /// <param name="url">请求地址</param>
  276. /// <param name="postData"></param>
  277. /// <returns>请求结果</returns>
  278. private static HttpWebRequest CreatePostHttpRequest(string url, string postData)
  279. {
  280. var postrequest = WebRequest.Create(url) as HttpWebRequest;
  281. //postrequest.KeepAlive = false;
  282. postrequest.Timeout = Timeout;
  283. postrequest.Method = "POST";
  284. postrequest.ContentType = "application/json;charset=UTF-8";
  285. //postrequest.ContentLength = postData.Length;
  286. //postrequest.AllowWriteStreamBuffering = false;
  287. //StreamWriter writer = new StreamWriter(postrequest.GetRequestStream(), Encoding.UTF8);
  288. //writer.Write(postData);
  289. //writer.Flush();
  290. byte[] data = Encoding.UTF8.GetBytes(postData);
  291. using (Stream reqStream = postrequest.GetRequestStream())
  292. {
  293. reqStream.Write(data, 0, data.Length);
  294. reqStream.Close();
  295. }
  296. return postrequest;
  297. }
  298. public static HttpWebRequest CreateDeleteHttpRequest(string url, string postJson, int _timeout = 5000)
  299. {
  300. var deleteRequest = WebRequest.Create(url) as HttpWebRequest;
  301. deleteRequest.Timeout = _timeout;
  302. deleteRequest.Method = "DELETE";
  303. deleteRequest.ContentType = "application/json;charset=UTF-8";
  304. byte[] data = Encoding.UTF8.GetBytes(postJson);
  305. using (Stream reqStream = deleteRequest.GetRequestStream())
  306. {
  307. reqStream.Write(data, 0, data.Length);
  308. reqStream.Close();
  309. }
  310. return deleteRequest;
  311. }
  312. #endregion
  313. }
  314. }