APICaller.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Newtonsoft.Json;
  2. using ServiceCenter.Extensions;
  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. LogHub.Publish($"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口调用日志", $"调用日志.txt", $"{DateTime.Now.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--接口{url}传参---------{JsonConvert.SerializeObject(jsonstr)}\n");
  77. var sw = new Stopwatch();
  78. sw.Start();
  79. Encoding encoding = Encoding.UTF8;
  80. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
  81. request.Timeout = 60000;//连接超时
  82. request.ReadWriteTimeout = 3600000;//读写超时
  83. request.Accept = "text/html,application/xhtml+xml,*/*";
  84. request.ContentType = "application/json";
  85. request.Method = type.ToUpper().ToString();//get或者post
  86. byte[] buffer = encoding.GetBytes(jsonstr);
  87. request.ContentLength = buffer.Length;
  88. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  89. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  90. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  91. {
  92. var res = reader.ReadToEnd();
  93. sw.Stop();
  94. LogHub.Publish($"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口调用日志", $"调用日志.txt", $"{DateTime.Now.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--接口{url}调用耗时:{sw.ElapsedMilliseconds}---------{JsonConvert.SerializeObject(res)}\n");
  95. return res;
  96. }
  97. }
  98. }
  99. }