HttpClientHelper.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Http;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace GxPress.Common.Http
  8. {
  9. /// <summary>
  10. /// HttpClient
  11. /// </summary>
  12. public static class HttpClientHelper
  13. {
  14. /// <summary>
  15. /// get
  16. /// </summary>
  17. /// <param name="url"></param>
  18. /// <param name="headers"></param>
  19. /// <returns></returns>
  20. public static async Task<HttpResponseMessage> GetHeadersAsync(string url, Dictionary<string, string> headers)
  21. {
  22. var httpClientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator };
  23. HttpClient httpClient = new HttpClient(httpClientHandler); //http对象
  24. //添加header
  25. foreach (var header in headers)
  26. {
  27. httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
  28. }
  29. var response = await httpClient.GetAsync(url);
  30. return response;
  31. }
  32. /// <summary>
  33. /// 根据地址下载
  34. /// </summary>
  35. /// <param name="url"></param>
  36. /// <returns></returns>
  37. public static async Task<byte[]> GetByteArrayAsync(string url)
  38. {
  39. var httpClientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator };
  40. HttpClient httpClient = new HttpClient(httpClientHandler); //http对象
  41. var response = await httpClient.GetByteArrayAsync(url);
  42. return response;
  43. }
  44. /// <summary>
  45. /// post
  46. /// </summary>
  47. /// <param name="url"></param>
  48. /// <param name="jsonObj">json</param>
  49. /// <param name="headers"></param>
  50. /// <returns></returns>
  51. public static async Task<HttpResponseMessage> PostAsync(string url, object jsonObj,
  52. Dictionary<string, string> headers = null)
  53. {
  54. var httpClientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator };
  55. HttpClient httpClient = new HttpClient(httpClientHandler); //http对象
  56. if (headers != null)
  57. foreach (var header in headers)
  58. {
  59. httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
  60. }
  61. HttpResponseMessage response = await httpClient.PostAsync(url,
  62. new JsonContent(jsonObj));
  63. return response;
  64. }
  65. /// <summary>
  66. /// rest删除
  67. /// </summary>
  68. /// <param name="url"></param>
  69. /// <param name="headers"></param>
  70. /// <returns></returns>
  71. public static async Task<HttpResponseMessage> DeleteAsync(string url, Dictionary<string, string> headers = null)
  72. {
  73. var httpClientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator };
  74. HttpClient httpClient = new HttpClient(httpClientHandler); //http对象
  75. if (headers != null)
  76. foreach (var header in headers)
  77. {
  78. httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
  79. }
  80. HttpResponseMessage response = await httpClient.DeleteAsync(url);
  81. return response;
  82. }
  83. /// <summary>
  84. /// 返回JSon数据
  85. /// </summary>
  86. /// <param name="JSONData">要处理的JSON数据</param>
  87. /// <param name="Url">要提交的URL</param>
  88. /// <returns>返回的JSON处理字符串</returns>
  89. public static string GetResponseData(string JSONData, string Url)
  90. {
  91. try
  92. {
  93. byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
  94. var request = (HttpWebRequest)WebRequest.Create(Url);
  95. request.Method = "POST";
  96. request.ContentLength = bytes.Length;
  97. request.ContentType = "text/xml";
  98. Stream reqstream = request.GetRequestStream();
  99. reqstream.Write(bytes, 0, bytes.Length);
  100. //声明一个HttpWebRequest请求
  101. request.Timeout = 90000;
  102. //设置连接超时时间
  103. //request.Headers.Set("Pragma", "no-cache");
  104. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  105. Stream streamReceive = response.GetResponseStream();
  106. Encoding encoding = Encoding.UTF8;
  107. StreamReader streamReader = new StreamReader(streamReceive, encoding);
  108. string strResult = streamReader.ReadToEnd();
  109. streamReceive.Dispose();
  110. streamReader.Dispose();
  111. return strResult;
  112. }
  113. catch (System.Exception)
  114. {
  115. return "";
  116. }
  117. return "";
  118. }
  119. }
  120. }