纽威
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.

204 lines
5.9 KiB

3 years ago
  1. using System.Collections.Generic;
  2. using System.Net;
  3. using System.Net.NetworkInformation;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Web;
  7. namespace NFine.Code
  8. {
  9. /// <summary>
  10. /// 网络操作
  11. /// </summary>
  12. public class Net
  13. {
  14. #region Ip(获取Ip)
  15. /// <summary>
  16. /// 获取Ip
  17. /// </summary>
  18. public static string Ip
  19. {
  20. get
  21. {
  22. var result = string.Empty;
  23. if (HttpContext.Current != null)
  24. result = GetWebClientIp();
  25. if (result.IsEmpty())
  26. result = GetLanIp();
  27. return result;
  28. }
  29. }
  30. /// <summary>
  31. /// 获取Web客户端的Ip
  32. /// </summary>
  33. private static string GetWebClientIp()
  34. {
  35. var ip = GetWebRemoteIp();
  36. foreach (var hostAddress in Dns.GetHostAddresses(ip))
  37. {
  38. if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
  39. return hostAddress.ToString();
  40. }
  41. return string.Empty;
  42. }
  43. /// <summary>
  44. /// 获取Web远程Ip
  45. /// </summary>
  46. private static string GetWebRemoteIp()
  47. {
  48. return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  49. }
  50. /// <summary>
  51. /// 获取局域网IP
  52. /// </summary>
  53. private static string GetLanIp()
  54. {
  55. foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
  56. {
  57. if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
  58. return hostAddress.ToString();
  59. }
  60. return string.Empty;
  61. }
  62. #endregion
  63. #region Host(获取主机名)
  64. /// <summary>
  65. /// 获取主机名
  66. /// </summary>
  67. public static string Host
  68. {
  69. get
  70. {
  71. return HttpContext.Current == null ? Dns.GetHostName() : GetWebClientHostName();
  72. }
  73. }
  74. /// <summary>
  75. /// 获取Web客户端主机名
  76. /// </summary>
  77. private static string GetWebClientHostName()
  78. {
  79. if (!HttpContext.Current.Request.IsLocal)
  80. return string.Empty;
  81. var ip = GetWebRemoteIp();
  82. var result = Dns.GetHostEntry(IPAddress.Parse(ip)).HostName;
  83. if (result == "localhost.localdomain")
  84. result = Dns.GetHostName();
  85. return result;
  86. }
  87. #endregion
  88. #region 获取mac地址
  89. /// <summary>
  90. /// 返回描述本地计算机上的网络接口的对象(网络接口也称为网络适配器)。
  91. /// </summary>
  92. /// <returns></returns>
  93. public static NetworkInterface[] NetCardInfo()
  94. {
  95. return NetworkInterface.GetAllNetworkInterfaces();
  96. }
  97. ///<summary>
  98. /// 通过NetworkInterface读取网卡Mac
  99. ///</summary>
  100. ///<returns></returns>
  101. public static List<string> GetMacByNetworkInterface()
  102. {
  103. List<string> macs = new List<string>();
  104. NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
  105. foreach (NetworkInterface ni in interfaces)
  106. {
  107. macs.Add(ni.GetPhysicalAddress().ToString());
  108. }
  109. return macs;
  110. }
  111. #endregion
  112. #region Ip城市(获取Ip城市)
  113. /// <summary>
  114. /// 获取IP地址信息
  115. /// </summary>
  116. /// <param name="ip"></param>
  117. /// <returns></returns>
  118. public static string GetLocation(string ip)
  119. {
  120. string res = "";
  121. try
  122. {
  123. string url = "http://apis.juhe.cn/ip/ip2addr?ip=" + ip + "&dtype=json&key=b39857e36bee7a305d55cdb113a9d725";
  124. res = HttpMethods.HttpGet(url);
  125. var resjson = res.ToObject<objex>();
  126. res = resjson.result.area + " " + resjson.result.location;
  127. }
  128. catch
  129. {
  130. res = "";
  131. }
  132. if (!string.IsNullOrEmpty(res))
  133. {
  134. return res;
  135. }
  136. try
  137. {
  138. string url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=" + ip + "&resource_id=6006&ie=utf8&oe=gbk&format=json";
  139. res = HttpMethods.HttpGet(url, Encoding.GetEncoding("GBK"));
  140. var resjson = res.ToObject<obj>();
  141. res = resjson.data[0].location;
  142. }
  143. catch
  144. {
  145. res = "";
  146. }
  147. return res;
  148. }
  149. /// <summary>
  150. /// 百度接口
  151. /// </summary>
  152. public class obj
  153. {
  154. public List<dataone> data { get; set; }
  155. }
  156. public class dataone
  157. {
  158. public string location { get; set; }
  159. }
  160. /// <summary>
  161. /// 聚合数据
  162. /// </summary>
  163. public class objex
  164. {
  165. public string resultcode { get; set; }
  166. public dataoneex result { get; set; }
  167. public string reason { get; set; }
  168. public string error_code { get; set; }
  169. }
  170. public class dataoneex
  171. {
  172. public string area { get; set; }
  173. public string location { get; set; }
  174. }
  175. #endregion
  176. #region Browser(获取浏览器信息)
  177. /// <summary>
  178. /// 获取浏览器信息
  179. /// </summary>
  180. public static string Browser
  181. {
  182. get
  183. {
  184. if (HttpContext.Current == null)
  185. return string.Empty;
  186. var browser = HttpContext.Current.Request.Browser;
  187. return string.Format("{0} {1}", browser.Browser, browser.Version);
  188. }
  189. }
  190. #endregion
  191. }
  192. }