HttpHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Business.Core.Utilities
  7. {
  8. public class HttpHelper
  9. {/// <summary>
  10. /// 发起POST同步请求
  11. ///
  12. /// </summary>
  13. /// <param name="url"></param>
  14. /// <param name="postData"></param>
  15. /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  16. /// <param name="headers">填充消息头</param>
  17. /// <returns></returns>
  18. public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
  19. {
  20. postData = postData ?? "";
  21. using (HttpClient client = new HttpClient())
  22. {
  23. if (headers != null)
  24. {
  25. foreach (var header in headers)
  26. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  27. }
  28. using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
  29. {
  30. if (contentType != null)
  31. httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  32. HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
  33. return response.Content.ReadAsStringAsync().Result;
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// 发起POST异步请求
  39. /// </summary>
  40. /// <param name="url"></param>
  41. /// <param name="postData"></param>
  42. /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  43. /// <param name="headers">填充消息头</param>
  44. /// <returns></returns>
  45. public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
  46. {
  47. postData = postData ?? "";
  48. using (HttpClient client = new HttpClient())
  49. {
  50. client.Timeout = new TimeSpan(0, 0, timeOut);
  51. if (headers != null)
  52. {
  53. foreach (var header in headers)
  54. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  55. }
  56. using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
  57. {
  58. if (contentType != null)
  59. httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  60. HttpResponseMessage response = await client.PostAsync(url, httpContent);
  61. return await response.Content.ReadAsStringAsync();
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// 发起GET同步请求
  67. /// </summary>
  68. /// <param name="url"></param>
  69. /// <param name="headers"></param>
  70. /// <param name="contentType"></param>
  71. /// <returns></returns>
  72. public static string HttpGet(string url, Dictionary<string, string> headers = null)
  73. {
  74. using (HttpClient client = new HttpClient())
  75. {
  76. if (headers != null)
  77. {
  78. foreach (var header in headers)
  79. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  80. }
  81. HttpResponseMessage response = client.GetAsync(url).Result;
  82. return response.Content.ReadAsStringAsync().Result;
  83. }
  84. }
  85. /// <summary>
  86. /// 发起GET异步请求
  87. /// </summary>
  88. /// <param name="url"></param>
  89. /// <param name="headers"></param>
  90. /// <param name="contentType"></param>
  91. /// <returns></returns>
  92. public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
  93. {
  94. using (HttpClient client = new HttpClient())
  95. {
  96. if (headers != null)
  97. {
  98. foreach (var header in headers)
  99. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  100. }
  101. HttpResponseMessage response = await client.GetAsync(url);
  102. return await response.Content.ReadAsStringAsync();
  103. }
  104. }
  105. }
  106. }