APICaller.cs 3.8 KB

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