APICaller.cs 4.3 KB

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