APICaller.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Logs;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Net;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using static System.Net.WebRequest;
  12. namespace WCS.Service
  13. {
  14. public class APICaller
  15. {
  16. private class Result
  17. {
  18. public bool Succsess;
  19. public string Exception;
  20. public object Data;
  21. }
  22. private static ConcurrentDictionary<string, Result> Results = new ConcurrentDictionary<string, Result>();
  23. public static T CallApi2<T>(string url, object parameter, string type = "POST")
  24. {
  25. var content = JsonConvert.SerializeObject(parameter);
  26. var result = HttpApi(url, content, type);
  27. return JsonConvert.DeserializeObject<T>(result);
  28. }
  29. public static T CallApi<T>(string url, object parameter, string type = "Post")
  30. {
  31. var content = JsonConvert.SerializeObject(parameter);
  32. var key = url + content;
  33. if (Results.ContainsKey(key))
  34. {
  35. var res = Results[key];
  36. if (res == null)
  37. throw new WarnException("接口调用中");
  38. try
  39. {
  40. if (!res.Succsess)
  41. throw new WarnException(res.Exception);
  42. return (T)res.Data;
  43. }
  44. finally
  45. {
  46. Results.Remove(key, out res);
  47. }
  48. }
  49. else
  50. {
  51. Results[key] = null;
  52. var task = Task.Run(() =>
  53. {
  54. var res = new Result();
  55. try
  56. {
  57. var result = HttpApi(url, content, type);
  58. res.Data = JsonConvert.DeserializeObject<T>(result);
  59. res.Succsess = true;
  60. }
  61. catch (Exception ex)
  62. {
  63. res.Exception = ex.Message;
  64. }
  65. finally
  66. {
  67. Results[key] = res;
  68. }
  69. });
  70. task.Wait(50);
  71. if (task.IsCompleted)
  72. {
  73. return CallApi<T>(url, parameter, type);
  74. }
  75. else
  76. {
  77. throw new WarnException("接口调用中");
  78. }
  79. }
  80. }
  81. private static string HttpApi(string url, string jsonstr, string type)
  82. {
  83. var sw = new Stopwatch();
  84. sw.Start();
  85. var encoding = Encoding.UTF8;
  86. #pragma warning disable SYSLIB0014
  87. var request = (HttpWebRequest)Create(url);//webrequest请求api地址
  88. #pragma warning restore SYSLIB0014
  89. request.Timeout = 60000;//连接超时
  90. request.ReadWriteTimeout = 3600000;//读写超时
  91. request.Accept = "text/html,application/xhtml+xml,*/*";
  92. request.ContentType = "application/json";
  93. request.Method = type.ToUpper().ToString();//get或者post
  94. var buffer = encoding.GetBytes(jsonstr);
  95. request.ContentLength = buffer.Length;
  96. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  97. var response = (HttpWebResponse)request.GetResponse();
  98. using var reader = new StreamReader(response.GetResponseStream()!, Encoding.UTF8);
  99. var res = reader.ReadToEnd();
  100. sw.Stop();
  101. if (sw.ElapsedMilliseconds > 500)
  102. InfoLog.INFO_TIMING($"接口{url}调用耗时{sw.ElapsedMilliseconds}---------{JsonConvert.SerializeObject(res)}");
  103. return res;
  104. }
  105. }
  106. }