Web.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System.Net;
  2. using System.Text;
  3. using RestSharp;
  4. namespace EasyTemplate.Tool;
  5. public class Web
  6. {
  7. /// <summary>
  8. /// 发送GET请求
  9. /// </summary>
  10. /// <param name="url">请求地址</param>
  11. /// <param name="headers">请求头</param>
  12. /// <returns></returns>
  13. public static string Get(string url, Dictionary<string, string> headers = null)
  14. {
  15. var client = new RestClient(url);
  16. var request = new RestRequest(Method.GET);
  17. if (headers != null)
  18. {
  19. foreach (var header in headers)
  20. {
  21. request.AddHeader(header.Key, header.Value);
  22. }
  23. }
  24. var response = client.Execute(request);
  25. return response.Content;
  26. }
  27. /// <summary>
  28. /// 发送POST请求
  29. /// </summary>
  30. /// <param name="url">请求地址</param>
  31. /// <param name="body">请求体</param>
  32. /// <param name="headers">请求头</param>
  33. /// <returns></returns>
  34. public static string Post(string url, object body, Dictionary<string, string> headers = null)
  35. {
  36. var client = new RestClient(url);
  37. var request = new RestRequest(Method.POST);
  38. if (headers != null)
  39. {
  40. foreach (var header in headers)
  41. {
  42. request.AddHeader(header.Key, header.Value);
  43. }
  44. }
  45. request.AddJsonBody(body);
  46. var response = client.Execute(request);
  47. return response.Content;
  48. }
  49. /// <summary>
  50. /// 发送PUT请求
  51. /// </summary>
  52. /// <param name="url">请求地址</param>
  53. /// <param name="body">请求体</param>
  54. /// <param name="headers">请求头</param>
  55. /// <returns></returns>
  56. public static string Put(string url, object body, Dictionary<string, string> headers = null)
  57. {
  58. var client = new RestClient(url);
  59. var request = new RestRequest(Method.PUT);
  60. if (headers != null)
  61. {
  62. foreach (var header in headers)
  63. {
  64. request.AddHeader(header.Key, header.Value);
  65. }
  66. }
  67. request.AddJsonBody(body);
  68. var response = client.Execute(request);
  69. return response.Content;
  70. }
  71. /// <summary>
  72. ///
  73. /// </summary>
  74. /// <returns></returns>
  75. public static string GetWanIp()
  76. {
  77. string ip = string.Empty;
  78. try
  79. {
  80. string url = "http://www.net.cn/static/customercare/yourip.asp";
  81. string html = "";
  82. using (var client = new HttpClient())
  83. {
  84. var reponse = client.GetAsync(url).GetAwaiter().GetResult();
  85. reponse.EnsureSuccessStatusCode();
  86. html = reponse.Content.ReadAsStringAsync().GetAwaiter().GetResult();
  87. }
  88. if (!string.IsNullOrEmpty(html))
  89. {
  90. ip = Resove(html, "<h2>", "</h2>");
  91. }
  92. }
  93. catch (Exception)
  94. {
  95. return string.Empty;
  96. }
  97. return ip;
  98. }
  99. /// <summary>
  100. /// Get part Content from HTML by apply prefix part and subfix part
  101. /// </summary>
  102. /// <param name="html">souce html</param>
  103. /// <param name="prefix">prefix</param>
  104. /// <param name="subfix">subfix</param>
  105. /// <returns>part content</returns>
  106. public static string Resove(string html, string prefix, string subfix)
  107. {
  108. int inl = html.IndexOf(prefix);
  109. if (inl == -1)
  110. {
  111. return null;
  112. }
  113. inl += prefix.Length;
  114. int inl2 = html.IndexOf(subfix, inl);
  115. string s = html.Substring(inl, inl2 - inl);
  116. return s;
  117. }
  118. public static string GetIpLocation(string ipAddress)
  119. {
  120. string ipLocation = "未知";
  121. try
  122. {
  123. if (!IsInnerIP(ipAddress))
  124. {
  125. ipLocation = GetIpLocationFromPCOnline(ipAddress);
  126. }
  127. else
  128. {
  129. ipLocation = "本地局域网";
  130. }
  131. }
  132. catch (Exception)
  133. {
  134. return ipLocation;
  135. }
  136. return ipLocation;
  137. }
  138. private static string GetIpLocationFromPCOnline(string ipAddress)
  139. {
  140. string ipLocation = "未知";
  141. try
  142. {
  143. var res = "";
  144. using (var client = new HttpClient())
  145. {
  146. var URL = "http://whois.pconline.com.cn/ip.jsp?ip=" + ipAddress;
  147. var response = client.GetAsync(URL).GetAwaiter().GetResult();
  148. response.EnsureSuccessStatusCode();
  149. res = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
  150. }
  151. if (!string.IsNullOrEmpty(res))
  152. {
  153. ipLocation = res.Trim();
  154. }
  155. }
  156. catch
  157. {
  158. ipLocation = "未知";
  159. }
  160. return ipLocation;
  161. }
  162. public static bool IsInnerIP(string ipAddress)
  163. {
  164. bool isInnerIp = false;
  165. long ipNum = GetIpNum(ipAddress);
  166. /**
  167. 私有IP:A类 10.0.0.0-10.255.255.255
  168. B类 172.16.0.0-172.31.255.255
  169. C类 192.168.0.0-192.168.255.255
  170. 当然,还有127这个网段是环回地址
  171. **/
  172. long aBegin = GetIpNum("10.0.0.0");
  173. long aEnd = GetIpNum("10.255.255.255");
  174. long bBegin = GetIpNum("172.16.0.0");
  175. long bEnd = GetIpNum("172.31.255.255");
  176. long cBegin = GetIpNum("192.168.0.0");
  177. long cEnd = GetIpNum("192.168.255.255");
  178. isInnerIp = IsInner(ipNum, aBegin, aEnd) || IsInner(ipNum, bBegin, bEnd) || IsInner(ipNum, cBegin, cEnd) || ipAddress.Equals("127.0.0.1");
  179. return isInnerIp;
  180. }
  181. /// <summary>
  182. /// 把IP地址转换为Long型数字
  183. /// </summary>
  184. /// <param name="ipAddress">IP地址字符串</param>
  185. /// <returns></returns>
  186. private static long GetIpNum(string ipAddress)
  187. {
  188. string[] ip = ipAddress.Split('.');
  189. long a = int.Parse(ip[0]);
  190. long b = int.Parse(ip[1]);
  191. long c = int.Parse(ip[2]);
  192. long d = int.Parse(ip[3]);
  193. long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
  194. return ipNum;
  195. }
  196. private static bool IsInner(long userIp, long begin, long end)
  197. {
  198. return (userIp >= begin) && (userIp <= end);
  199. }
  200. }