HttpHelper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Http;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace TEAMModelOS.SDK.Helper.Network.HttpHelper
  7. {
  8. public class HttpHelper
  9. {
  10. /// <summary>
  11. /// 同步GET请求
  12. /// </summary>
  13. /// <param name="url"></param>
  14. /// <param name="headers"></param>
  15. /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
  16. /// <returns></returns>
  17. public static string HttpGet(string url, Dictionary<string, string> headers = null, int timeout = 0)
  18. {
  19. HttpClient client = new HttpClient();
  20. if (headers != null)
  21. {
  22. foreach (KeyValuePair<string, string> header in headers)
  23. {
  24. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  25. }
  26. }
  27. if (timeout > 0)
  28. {
  29. client.Timeout = new TimeSpan(0, 0, timeout);
  30. }
  31. Byte[] resultBytes = client.GetByteArrayAsync(url).Result;
  32. return Encoding.UTF8.GetString(resultBytes);
  33. }
  34. /// <summary>
  35. /// 异步GET请求
  36. /// </summary>
  37. /// <param name="url"></param>
  38. /// <param name="headers"></param>
  39. /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
  40. /// <returns></returns>
  41. public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null, int timeout = 0)
  42. {
  43. HttpClient client = new HttpClient();
  44. if (headers != null)
  45. {
  46. foreach (KeyValuePair<string, string> header in headers)
  47. {
  48. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  49. }
  50. }
  51. if (timeout > 0)
  52. {
  53. client.Timeout = new TimeSpan(0, 0, timeout);
  54. }
  55. Byte[] resultBytes = await client.GetByteArrayAsync(url);
  56. return Encoding.Default.GetString(resultBytes);
  57. }
  58. /// <summary>
  59. /// 同步POST请求
  60. /// </summary>
  61. /// <param name="url"></param>
  62. /// <param name="postData"></param>
  63. /// <param name="headers"></param>
  64. /// <param name="contentType"></param>
  65. /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
  66. /// <param name="encoding">默认UTF8</param>
  67. /// <returns></returns>
  68. public static string HttpPost(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
  69. {
  70. HttpClient client = new HttpClient();
  71. if (headers != null)
  72. {
  73. foreach (KeyValuePair<string, string> header in headers)
  74. {
  75. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  76. }
  77. }
  78. if (timeout > 0)
  79. {
  80. client.Timeout = new TimeSpan(0, 0, timeout);
  81. }
  82. HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8);
  83. if (contentType != null)
  84. {
  85. content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  86. }
  87. HttpResponseMessage responseMessage = client.PostAsync(url, content).Result;
  88. Byte[] resultBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
  89. return Encoding.UTF8.GetString(resultBytes);
  90. }
  91. /// <summary>
  92. /// 异步POST请求
  93. /// </summary>
  94. /// <param name="url"></param>
  95. /// <param name="postData"></param>
  96. /// <param name="headers"></param>
  97. /// <param name="contentType"></param>
  98. /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
  99. /// <param name="encoding">默认UTF8</param>
  100. /// <returns></returns>
  101. public static async Task<string> HttpPostAsync(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
  102. {
  103. HttpClient client = new HttpClient();
  104. if (headers != null)
  105. {
  106. foreach (KeyValuePair<string, string> header in headers)
  107. {
  108. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  109. }
  110. }
  111. if (timeout > 0)
  112. {
  113. client.Timeout = new TimeSpan(0, 0, timeout);
  114. }
  115. HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8);
  116. if (contentType != null)
  117. {
  118. content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  119. }
  120. HttpResponseMessage responseMessage = await client.PostAsync(url, content);
  121. Byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync();
  122. return Encoding.UTF8.GetString(resultBytes);
  123. }
  124. /// <summary>
  125. /// 异步POST请求
  126. /// </summary>
  127. /// <param name="url"></param>
  128. /// <param name="postData"></param>
  129. /// <param name="headers"></param>
  130. /// <param name="contentType"></param>
  131. /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
  132. /// <param name="encoding">默认UTF8</param>
  133. /// <returns></returns>
  134. public static async Task<string> HttpPostAsync(string url, List<KeyValuePair<string, string>> postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
  135. {
  136. HttpClient client = new HttpClient();
  137. if (headers != null)
  138. {
  139. foreach (KeyValuePair<string, string> header in headers)
  140. {
  141. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  142. }
  143. }
  144. if (timeout > 0)
  145. {
  146. client.Timeout = new TimeSpan(0, 0, timeout);
  147. }
  148. HttpContent content = new FormUrlEncodedContent(postData);
  149. if (contentType != null)
  150. {
  151. content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  152. }
  153. HttpResponseMessage responseMessage = await client.PostAsync(url, content);
  154. Byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync();
  155. return Encoding.UTF8.GetString(resultBytes);
  156. }
  157. }
  158. }