ApiHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Configuration;
  9. namespace CP.Common
  10. {
  11. public class ApiHelper
  12. {
  13. #region 配置
  14. private static string _url = null;
  15. public ApiHelper(string url)
  16. {
  17. _url = url;
  18. if (string.IsNullOrEmpty(url))
  19. url = ConfigurationManager.AppSettings["ZxUrl"];
  20. }
  21. public static string GetUrl
  22. {
  23. get
  24. {
  25. if (string.IsNullOrEmpty(_url))
  26. return ConfigurationManager.AppSettings["ZxUrl"];
  27. return _url;
  28. }
  29. }
  30. /// <summary>
  31. /// 资讯接口验证配置
  32. /// </summary>
  33. public static Dictionary<string, string> ZXHeader
  34. {
  35. get
  36. {
  37. return Nested.zxHeader;
  38. }
  39. }
  40. class Nested
  41. {
  42. internal static readonly Dictionary<string, string> zxHeader = new Dictionary<string, string> {
  43. { "Code",ConfigurationManager.AppSettings["ZxCode"] }
  44. };
  45. }
  46. #endregion
  47. /// <summary>
  48. /// get请求
  49. /// </summary>
  50. /// <param name="url"></param>
  51. /// <returns></returns>
  52. public static string Get(string url, Dictionary<string, string> header = null)
  53. {
  54. var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate };//文件类型
  55. using (var http = new HttpClient(handler))
  56. {
  57. AddHeader(http, header);
  58. var response = http.GetStringAsync(GetUrl + url);//请求
  59. var data = response.Result.ToString().JsonToT<BaseJsonResult>();
  60. if (data.Code == 200)
  61. return data.Data.ToString();
  62. else
  63. return string.Empty;
  64. }
  65. }
  66. /// <summary>
  67. /// post请求
  68. /// </summary>
  69. /// <param name="url"></param>
  70. /// <param name="jsonObj"></param>
  71. /// <returns></returns>
  72. public static string Post(string url, object jsonObj, Dictionary<string, string> header = null)
  73. {
  74. var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate };//文件类型
  75. using (var http = new HttpClient(handler))
  76. {
  77. AddHeader(http, header);
  78. var response = http.PostAsJsonAsync(GetUrl + url, jsonObj).Result;
  79. response.EnsureSuccessStatusCode();
  80. var data = response.Content.ReadAsStringAsync().Result.ToString().JsonToT<BaseJsonResult>();
  81. if (data.Code == 200)
  82. return data.Data.ToString();
  83. else
  84. return string.Empty;
  85. }
  86. }
  87. private static void AddHeader(HttpClient http, Dictionary<string, string> header)
  88. {
  89. if (header == null)
  90. return;
  91. foreach (var item in header)
  92. {
  93. http.DefaultRequestHeaders.Add(item.Key, item.Value);//添加headers
  94. }
  95. }
  96. }
  97. public class BaseJsonResult
  98. {
  99. /// <summary>
  100. /// 状态编码
  101. /// </summary>
  102. public int Code { get; set; }
  103. /// <summary>
  104. /// 数据
  105. /// </summary>
  106. public object Data { get; set; }
  107. /// <summary>
  108. /// 返回信息
  109. /// </summary>
  110. public string Message { get; set; }
  111. }
  112. }