APICaller.cs 3.7 KB

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