APICaller.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Newtonsoft.Json;
  2. using System.Collections.Concurrent;
  3. using System.Diagnostics;
  4. using System.Net;
  5. using System.Text;
  6. using WCS.Core;
  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 KnownException("接口调用中", LogLevel.Low);
  32. try
  33. {
  34. if (!res.Succsess) throw new KnownException(res.Exception, LogLevel.Mid);
  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 KnownException("接口调用中", LogLevel.Low);
  71. }
  72. }
  73. }
  74. private static string HttpApi(string url, string jsonstr, string type)
  75. {
  76. var sw = new Stopwatch();
  77. sw.Start();
  78. Encoding encoding = Encoding.UTF8;
  79. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
  80. request.Timeout = 60000;//连接超时
  81. request.ReadWriteTimeout = 3600000;//读写超时
  82. request.Accept = "text/html,application/xhtml+xml,*/*";
  83. request.ContentType = "application/json";
  84. request.Method = type.ToUpper().ToString();//get或者post
  85. byte[] buffer = encoding.GetBytes(jsonstr);
  86. request.ContentLength = buffer.Length;
  87. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  88. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  89. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  90. {
  91. var res = reader.ReadToEnd();
  92. sw.Stop();
  93. if (sw.ElapsedMilliseconds > 500) Ltc.Log($"接口{url}调用耗时{sw.ElapsedMilliseconds}---------{JsonConvert.SerializeObject(res)}", LogLevel.Low, ErrorType.Kown);
  94. return res;
  95. }
  96. }
  97. }
  98. }