HttpHelper.cs 5.8 KB

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