华恒Mes鼎捷代码
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.

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