APICaller.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 CallApi1<T>(string url, string parameter, string type = "GET")
  25. {
  26. var result = HttpApi(url + parameter, "", 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) throw new Exception("接口调用中");
  37. try
  38. {
  39. if (!res.Succsess) throw new Exception(res.Exception);
  40. return (T)res.Data;
  41. }
  42. finally
  43. {
  44. Results.Remove(key, out res);
  45. }
  46. }
  47. else
  48. {
  49. Results[key] = null;
  50. var task = Task.Run(() =>
  51. {
  52. var res = new Result();
  53. try
  54. {
  55. var result = HttpApi(url, content, type);
  56. res.Data = JsonConvert.DeserializeObject<T>(result);
  57. res.Succsess = true;
  58. }
  59. catch (Exception ex)
  60. {
  61. res.Exception = ex.Message;
  62. }
  63. finally
  64. {
  65. Results[key] = res;
  66. }
  67. });
  68. task.Wait(50);
  69. if (task.IsCompleted)
  70. {
  71. return CallApi<T>(url, parameter, type);
  72. }
  73. else
  74. {
  75. throw new Exception("接口调用中");
  76. }
  77. }
  78. }
  79. private static string HttpApi(string url, string jsonstr, string type)
  80. {
  81. var name = url.Split("/").Last();
  82. LogHub.InterfacePublish(name, $"[{Thread.CurrentThread.ManagedThreadId}]--接口{name}传参---------{JsonConvert.SerializeObject(jsonstr)}");
  83. try
  84. {
  85. var sw = new Stopwatch();
  86. sw.Start();
  87. Encoding encoding = Encoding.UTF8;
  88. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
  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. byte[] buffer = encoding.GetBytes(jsonstr);
  95. request.ContentLength = buffer.Length;
  96. if (type != "GET")
  97. {
  98. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  99. }
  100. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  101. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  102. {
  103. var res = reader.ReadToEnd();
  104. sw.Stop();
  105. LogHub.InterfacePublish(name, $"[{Thread.CurrentThread.ManagedThreadId}]--接口{name}调用耗时:{sw.ElapsedMilliseconds}---------{JsonConvert.SerializeObject(res)}");
  106. return res;
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. LogHub.InterfacePublish(name, $"[{Thread.CurrentThread.ManagedThreadId}]--接口{name}调用错误:{ex.StackTrace}");
  112. throw new Exception(ex.Message);
  113. }
  114. }
  115. }
  116. }