HttpHelper.cs 4.9 KB

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