APICaller.cs 3.7 KB

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