终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

HttpRequestHelper.cs 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BPASmartClient.Compiler
  8. {
  9. /// <summary>
  10. /// 该类实现客户端http 同步请求
  11. /// 支持环境 -.net4.0/-.net4.5
  12. /// 创建人:奉友福
  13. /// </summary>
  14. public class HttpRequestHelper
  15. {
  16. #region 私有变量
  17. #endregion
  18. #region 公用函数
  19. /// <summary>
  20. /// GET 同步请求
  21. /// 创建人:奉友福
  22. /// 创建时间:2020-11-19
  23. /// </summary>
  24. /// <param name="url">请求地址</param>
  25. /// <returns>超时时间设置,默认5秒</returns>
  26. public static string HttpGetRequest(string url,int _timeout = 2000)
  27. {
  28. string resultData = string.Empty;
  29. try
  30. {
  31. WebClient wc = new WebClient();
  32. byte[] bytes = wc.DownloadData(url);
  33. string s = Encoding.UTF8.GetString(bytes);
  34. return s;
  35. }
  36. catch (Exception e)
  37. {
  38. throw e;
  39. }
  40. return "";
  41. try
  42. {
  43. var getrequest = HttpRequest.GetInstance().CreateHttpRequest(url,"GET",_timeout);
  44. var getreponse = getrequest.GetResponse() as HttpWebResponse;
  45. resultData = HttpRequest.GetInstance().GetHttpResponse(getreponse,"GET");
  46. }
  47. catch (Exception)
  48. {
  49. throw;
  50. }
  51. return resultData;
  52. }
  53. /// <summary>
  54. /// POST 同步请求
  55. /// 创建人:奉友福
  56. /// 创建时间:2020-11-19
  57. /// </summary>
  58. /// <param name="url">请求地址</param>
  59. /// <param name="PostJsonData">请求数据</param>
  60. /// <returns></returns>
  61. public static string HttpPostRequest(string url,string PostJsonData,int _timeout = 2000)
  62. {
  63. string resultData = string.Empty;
  64. try
  65. {
  66. var postrequest = HttpRequest.GetInstance().CreateHttpRequest(url,"POST",_timeout,PostJsonData);
  67. var postreponse = postrequest.GetResponse() as HttpWebResponse;
  68. resultData = HttpRequest.GetInstance().GetHttpResponse(postreponse,"POST");
  69. }
  70. catch (Exception ex)
  71. {
  72. return ex.Message;
  73. }
  74. return resultData;
  75. }
  76. public static string HttpDeleteRequest(string url,string PostJsonData,int _timeout = 10000)
  77. {
  78. string resultData = string.Empty;
  79. try
  80. {
  81. var deleteRequest = HttpRequest.CreateDeleteHttpRequest(url,PostJsonData,_timeout);
  82. var deleteReponse = deleteRequest.GetResponse() as HttpWebResponse;
  83. using (StreamReader reader = new StreamReader(deleteReponse.GetResponseStream(),Encoding.GetEncoding("UTF-8")))
  84. {
  85. resultData = reader.ReadToEnd();
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. }
  91. return resultData;
  92. }
  93. /// <summary>
  94. /// GET 同步请求
  95. /// </summary>
  96. /// <param name="url">地址</param>
  97. /// <param name="head">头</param>
  98. /// <param name="headInfo">内容</param>
  99. /// <returns></returns>
  100. public static string GetHttpGetResponseWithHead(string url,HttpRequestHeader head,string headInfo)
  101. {
  102. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  103. request.Method = "GET";
  104. request.ContentType = "application/json;charset=UTF-8";
  105. request.Timeout = 6000;
  106. request.Headers.Set(head,headInfo);
  107. StreamReader sr = null;
  108. HttpWebResponse response = null;
  109. Stream stream = null;
  110. try
  111. {
  112. response = (HttpWebResponse)request.GetResponse();
  113. stream = response.GetResponseStream();
  114. sr = new StreamReader(stream,Encoding.GetEncoding("utf-8"));
  115. var resultData = sr.ReadToEnd();
  116. return resultData;
  117. }
  118. catch (Exception ex)
  119. {
  120. Console.WriteLine(url + " 访问失败:" + ex.Message);
  121. //return ex.Message;
  122. }
  123. finally
  124. {
  125. if (response != null)
  126. {
  127. response.Dispose();
  128. }
  129. if (stream != null)
  130. {
  131. stream.Dispose();
  132. }
  133. if (sr != null)
  134. {
  135. sr.Dispose();
  136. }
  137. }
  138. return null;
  139. }
  140. /// <summary>
  141. /// Post请求带Token
  142. /// 2021-2-2 by dulf
  143. /// </summary>
  144. /// <param name="url"></param>
  145. /// <param name="head"></param>
  146. /// <param name="headInfo"></param>
  147. /// <param name="postParam"></param>
  148. /// <returns></returns>
  149. public static string HttpPostResponseWithHead(string url,HttpRequestHeader head,string headInfo,string postParam,int Timeout = 6000)
  150. {
  151. string resultData = string.Empty;
  152. try
  153. {
  154. var postrequest = WebRequest.Create(url) as HttpWebRequest;
  155. postrequest.Timeout = Timeout;
  156. postrequest.Method = "POST";
  157. postrequest.ContentType = "application/json;charset=UTF-8";
  158. postrequest.Headers.Set(head,headInfo);
  159. byte[] data = Encoding.UTF8.GetBytes(postParam);
  160. using (Stream reqStream = postrequest.GetRequestStream())
  161. {
  162. reqStream.Write(data,0,data.Length);
  163. var postreponse = postrequest.GetResponse() as HttpWebResponse;
  164. resultData = HttpRequest.GetInstance().GetHttpResponse(postreponse,"POST");
  165. reqStream.Close();
  166. }
  167. return resultData;
  168. }
  169. catch (Exception ex)
  170. {
  171. Console.Write("请求<HttpPostResponseWithHead>异常:" + ex.Message);
  172. }
  173. return "";
  174. }
  175. #endregion
  176. }
  177. /// <summary>
  178. /// HTTP请求类
  179. /// </summary>
  180. public class HttpRequest
  181. {
  182. #region 私有变量
  183. /// <summary>
  184. /// http请求超时时间设置
  185. /// 默认值:5秒
  186. /// </summary>
  187. private static int Timeout = 5000;
  188. #endregion
  189. #region 单例模式
  190. private static HttpRequest _HttpRequest = null;
  191. public static HttpRequest GetInstance()
  192. {
  193. if (_HttpRequest == null)
  194. {
  195. _HttpRequest = new HttpRequest();
  196. }
  197. return _HttpRequest;
  198. }
  199. private HttpRequest()
  200. {
  201. }
  202. #endregion
  203. #region 公用函数
  204. /// <summary>
  205. /// 函数名称:创建http请求
  206. /// 创建人:奉友福
  207. /// 创建时间:2020-11-19
  208. /// 例如GET 请求: 地址 + "GET"
  209. /// 例如POST请求: 地址 + "POST" + JSON
  210. /// </summary>
  211. /// <param name="url">http请求地址</param>
  212. /// <param name="requestType">http请求方式:GET/POST</param>
  213. /// <param name="strjson">http请求附带数据</param>
  214. /// <returns></returns>
  215. public HttpWebRequest CreateHttpRequest(string url,string requestType,int _timeout = 5000,params object[] strjson)
  216. {
  217. HttpWebRequest request = null;
  218. const string get = "GET";
  219. const string post = "POST";
  220. Timeout = _timeout;
  221. if (string.Equals(requestType,get,StringComparison.OrdinalIgnoreCase))
  222. {
  223. request = CreateGetHttpRequest(url);
  224. }
  225. if (string.Equals(requestType,post,StringComparison.OrdinalIgnoreCase))
  226. {
  227. request = CreatePostHttpRequest(url,strjson[0].ToString());
  228. }
  229. return request;
  230. }
  231. /// <summary>
  232. /// http获取数据
  233. /// </summary>
  234. /// <param name="response"></param>
  235. /// <param name="requestType"></param>
  236. /// <returns></returns>
  237. public string GetHttpResponse(HttpWebResponse response,string requestType)
  238. {
  239. var resultData = string.Empty;
  240. const string post = "POST";
  241. string encoding = "UTF-8";
  242. if (string.Equals(requestType,post,StringComparison.OrdinalIgnoreCase))
  243. {
  244. encoding = response.ContentEncoding;
  245. if (encoding == null || encoding.Length < 1)
  246. encoding = "UTF-8";
  247. }
  248. using (StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.GetEncoding(encoding)))
  249. {
  250. resultData = reader.ReadToEnd();
  251. }
  252. return resultData;
  253. }
  254. #endregion
  255. #region 私有函数
  256. /// <summary>
  257. /// http+GET请求
  258. /// </summary>
  259. /// <param name="url">请求地址</param>
  260. /// <returns>请求结果</returns>
  261. private static HttpWebRequest CreateGetHttpRequest(string url)
  262. {
  263. var getrequest = WebRequest.Create(url) as HttpWebRequest;
  264. getrequest.Method = "GET";
  265. getrequest.Timeout = Timeout;
  266. getrequest.ContentType = "application/json;charset=UTF-8";
  267. getrequest.Proxy = null;
  268. getrequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
  269. return getrequest;
  270. }
  271. /// <summary>
  272. /// http+POST请求
  273. /// </summary>
  274. /// <param name="url">请求地址</param>
  275. /// <param name="postData"></param>
  276. /// <returns>请求结果</returns>
  277. private static HttpWebRequest CreatePostHttpRequest(string url,string postData)
  278. {
  279. var postrequest = WebRequest.Create(url) as HttpWebRequest;
  280. //postrequest.KeepAlive = false;
  281. postrequest.Timeout = Timeout;
  282. postrequest.Method = "POST";
  283. postrequest.ContentType = "application/json;charset=UTF-8";
  284. //postrequest.ContentLength = postData.Length;
  285. //postrequest.AllowWriteStreamBuffering = false;
  286. //StreamWriter writer = new StreamWriter(postrequest.GetRequestStream(), Encoding.UTF8);
  287. //writer.Write(postData);
  288. //writer.Flush();
  289. byte[] data = Encoding.UTF8.GetBytes(postData);
  290. using (Stream reqStream = postrequest.GetRequestStream())
  291. {
  292. reqStream.Write(data,0,data.Length);
  293. reqStream.Close();
  294. }
  295. return postrequest;
  296. }
  297. public static HttpWebRequest CreateDeleteHttpRequest(string url,string postJson,int _timeout = 5000)
  298. {
  299. var deleteRequest = WebRequest.Create(url) as HttpWebRequest;
  300. deleteRequest.Timeout = _timeout;
  301. deleteRequest.Method = "DELETE";
  302. deleteRequest.ContentType = "application/json;charset=UTF-8";
  303. byte[] data = Encoding.UTF8.GetBytes(postJson);
  304. using (Stream reqStream = deleteRequest.GetRequestStream())
  305. {
  306. reqStream.Write(data,0,data.Length);
  307. reqStream.Close();
  308. }
  309. return deleteRequest;
  310. }
  311. #endregion
  312. }
  313. }