APICaller.cs 4.2 KB

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