锐腾搅拌上料功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.6 KiB

5 months ago
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. namespace ICSSoft.Frame.APP.Helper
  9. {
  10. public class WebHelper
  11. {
  12. public static T HttpPost<T>(string url, string body, string tocken = null)
  13. {
  14. T t;
  15. try
  16. {
  17. Encoding encoding = Encoding.UTF8;
  18. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  19. request.Method = "POST";
  20. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  21. request.ContentType = "application/json; charset=utf-8";
  22. if (tocken != null)
  23. request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + tocken);
  24. byte[] buffer = encoding.GetBytes(body);
  25. request.ContentLength = buffer.Length;
  26. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  27. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  28. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  29. {
  30. t= JsonConvert.DeserializeObject<T>(reader.ReadToEnd());
  31. }
  32. return t;
  33. }
  34. catch (WebException webEx)
  35. {
  36. if (webEx.Response != null)
  37. {
  38. using (var errorResponse = (HttpWebResponse)webEx.Response)
  39. {
  40. using (Stream errorDataStream = errorResponse.GetResponseStream())
  41. {
  42. StreamReader reader = new StreamReader(errorDataStream);
  43. string errorResponseContent = reader.ReadToEnd();
  44. try
  45. {
  46. t = JsonConvert.DeserializeObject<T>(errorResponseContent);
  47. }
  48. catch (Exception ex)
  49. {
  50. throw new Exception(webEx.InnerException.Message);
  51. }
  52. return t;
  53. }
  54. }
  55. }
  56. else
  57. {
  58. throw new Exception(webEx.Message);
  59. }
  60. }
  61. }
  62. public static T HttpGet<T>(string url, string tocken = null)
  63. {
  64. T t;
  65. try
  66. {
  67. Encoding encoding = Encoding.UTF8;
  68. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  69. request.Method = "GET";
  70. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  71. request.ContentType = "application/json; charset=utf-8";
  72. if (tocken != null)
  73. request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + tocken);
  74. var res = request.GetResponse();
  75. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  76. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  77. {
  78. t = JsonConvert.DeserializeObject<T>(reader.ReadToEnd());
  79. }
  80. return t;
  81. }
  82. catch (WebException webEx)
  83. {
  84. if (webEx.Response != null)
  85. {
  86. using (var errorResponse = (HttpWebResponse)webEx.Response)
  87. {
  88. using (Stream errorDataStream = errorResponse.GetResponseStream())
  89. {
  90. StreamReader reader = new StreamReader(errorDataStream);
  91. string errorResponseContent = reader.ReadToEnd();
  92. try
  93. {
  94. t = JsonConvert.DeserializeObject<T>(errorResponseContent);
  95. }
  96. catch (Exception ex)
  97. {
  98. throw new Exception(webEx.InnerException.Message);
  99. }
  100. return t;
  101. }
  102. }
  103. }
  104. else
  105. {
  106. throw new Exception(webEx.Message);
  107. }
  108. }
  109. }
  110. }
  111. }