APICaller.cs 3.8 KB

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