HttpClientHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.IO;
  7. using System.Net.Http;
  8. using System.Net.Http.Headers;
  9. using System.Threading.Tasks;
  10. using System.Xml.Serialization;
  11. namespace LYFZ.WinAPI
  12. {
  13. public class HttpClientHelper
  14. {
  15. /// <summary>
  16. /// get请求
  17. /// </summary>
  18. /// <param name="url"></param>
  19. /// <returns></returns>
  20. public static string GetResponse(string url)
  21. {
  22. if (url.StartsWith("https"))
  23. System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
  24. HttpClient httpClient = new HttpClient();
  25. httpClient.DefaultRequestHeaders.Accept.Add(
  26. new MediaTypeWithQualityHeaderValue("application/json"));
  27. HttpResponseMessage response = httpClient.GetAsync(url).Result;
  28. if (response.IsSuccessStatusCode)
  29. {
  30. string result = response.Content.ReadAsStringAsync().Result;
  31. return result;
  32. }
  33. return null;
  34. }
  35. /// <summary>
  36. /// post请求
  37. /// </summary>
  38. /// <param name="url"></param>
  39. /// <param name="postData">post数据</param>
  40. /// <returns></returns>
  41. public static string PostResponse(string url, string postData)
  42. {
  43. if (url.StartsWith("https"))
  44. System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
  45. HttpContent httpContent = new StringContent(postData);
  46. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  47. HttpClient httpClient = new HttpClient();
  48. HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
  49. if (response.IsSuccessStatusCode)
  50. {
  51. string result = response.Content.ReadAsStringAsync().Result;
  52. return result;
  53. }
  54. return null;
  55. }
  56. /// <summary>
  57. /// V3接口全部为Xml形式,故有此方法
  58. /// </summary>
  59. /// <typeparam name="T"></typeparam>
  60. /// <param name="url"></param>
  61. /// <param name="xmlString"></param>
  62. /// <returns></returns>
  63. public static T PostXmlResponse<T>(string url, string xmlString)
  64. where T : class,new()
  65. {
  66. if (url.StartsWith("https"))
  67. System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
  68. HttpContent httpContent = new StringContent(xmlString);
  69. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  70. HttpClient httpClient = new HttpClient();
  71. T result = default(T);
  72. HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
  73. if (response.IsSuccessStatusCode)
  74. {
  75. Task<string> t = response.Content.ReadAsStringAsync();
  76. string s = t.Result;
  77. result = XmlDeserialize<T>(s);
  78. }
  79. return result;
  80. }
  81. /// <summary>
  82. /// 反序列化Xml
  83. /// </summary>
  84. /// <typeparam name="T"></typeparam>
  85. /// <param name="xmlString"></param>
  86. /// <returns></returns>
  87. public static T XmlDeserialize<T>(string xmlString)
  88. where T : class,new ()
  89. {
  90. try
  91. {
  92. XmlSerializer ser = new XmlSerializer(typeof(T));
  93. using (StringReader reader = new StringReader(xmlString))
  94. {
  95. return (T)ser.Deserialize(reader);
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. throw new Exception("XmlDeserialize发生异常:xmlString:" + xmlString + "异常信息:" + ex.Message);
  101. }
  102. }
  103. }
  104. }