HttpHelper.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Http;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Infrastructure
  7. {
  8. public class HttpHelper
  9. {
  10. /// <summary>
  11. /// 发起POST同步请求
  12. ///
  13. /// </summary>
  14. /// <param name="url"></param>
  15. /// <param name="postData"></param>
  16. /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  17. /// <param name="headers">填充消息头</param>
  18. /// <returns></returns>
  19. public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
  20. {
  21. postData = postData ?? "";
  22. using (HttpClient client = new HttpClient())
  23. {
  24. client.Timeout = new TimeSpan(0, 0, timeOut);
  25. if (headers != null)
  26. {
  27. foreach (var header in headers)
  28. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  29. }
  30. using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
  31. {
  32. if (contentType != null)
  33. httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  34. HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
  35. return response.Content.ReadAsStringAsync().Result;
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// 发起POST异步请求
  41. /// </summary>
  42. /// <param name="url"></param>
  43. /// <param name="postData"></param>
  44. /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  45. /// <param name="headers">填充消息头</param>
  46. /// <returns></returns>
  47. public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
  48. {
  49. postData = postData ?? "";
  50. using (HttpClient client = new HttpClient())
  51. {
  52. client.Timeout = new TimeSpan(0, 0, timeOut);
  53. if (headers != null)
  54. {
  55. foreach (var header in headers)
  56. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  57. }
  58. using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
  59. {
  60. if (contentType != null)
  61. httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  62. HttpResponseMessage response = await client.PostAsync(url, httpContent);
  63. return await response.Content.ReadAsStringAsync();
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// 发起GET同步请求
  69. /// </summary>
  70. /// <param name="url"></param>
  71. /// <param name="headers"></param>
  72. /// <param name="contentType"></param>
  73. /// <returns></returns>
  74. public static string HttpGet(string url, Dictionary<string, string> headers = null)
  75. {
  76. using (HttpClient client = new HttpClient())
  77. {
  78. if (headers != null)
  79. {
  80. foreach (var header in headers)
  81. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  82. }
  83. else
  84. {
  85. client.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded");
  86. client.DefaultRequestHeaders.Add("UserAgent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
  87. }
  88. try
  89. {
  90. HttpResponseMessage response = client.GetAsync(url).Result;
  91. return response.Content.ReadAsStringAsync().Result;
  92. }
  93. catch (Exception ex)
  94. {
  95. //TODO 打印日志
  96. Console.WriteLine($"[Http请求出错]{url}|{ex.Message}");
  97. }
  98. return "";
  99. }
  100. }
  101. /// <summary>
  102. /// 发起GET异步请求
  103. /// </summary>
  104. /// <param name="url"></param>
  105. /// <param name="headers"></param>
  106. /// <returns></returns>
  107. public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
  108. {
  109. using (HttpClient client = new HttpClient())
  110. {
  111. if (headers != null)
  112. {
  113. foreach (var header in headers)
  114. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  115. }
  116. HttpResponseMessage response = await client.GetAsync(url);
  117. return await response.Content.ReadAsStringAsync();
  118. }
  119. }
  120. }
  121. }