APICaller.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Net;
  7. using System.Text;
  8. using WCS.Core.Log;
  9. using static System.Net.WebRequest;
  10. namespace WCS.WebApi
  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. return (T)res.Data;
  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. private static string HttpApi(string url, string jsonstr, string type)
  80. {
  81. var sw = new Stopwatch();
  82. sw.Start();
  83. var encoding = Encoding.UTF8;
  84. #pragma warning disable SYSLIB0014
  85. var request = (HttpWebRequest)Create(url);//webrequest请求api地址
  86. #pragma warning restore SYSLIB0014
  87. request.Timeout = 60000;//连接超时
  88. request.ReadWriteTimeout = 3600000;//读写超时
  89. request.Accept = "text/html,application/xhtml+xml,*/*";
  90. request.ContentType = "application/json";
  91. request.Method = type.ToUpper().ToString();//get或者post
  92. var buffer = encoding.GetBytes(jsonstr);
  93. request.ContentLength = buffer.Length;
  94. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  95. var response = (HttpWebResponse)request.GetResponse();
  96. using var reader = new StreamReader(response.GetResponseStream()!, Encoding.UTF8);
  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. }