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

205 lines
6.0 KiB

1 year 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. return res;//ADD On 2020-10-12
  122. //try
  123. //{
  124. // string url = "http://apis.juhe.cn/ip/ip2addr?ip=" + ip + "&dtype=json&key=b39857e36bee7a305d55cdb113a9d725";
  125. // res = HttpMethods.HttpGet(url);
  126. // var resjson = res.ToObject<objex>();
  127. // res = resjson.result.area + " " + resjson.result.location;
  128. //}
  129. //catch
  130. //{
  131. // res = "";
  132. //}
  133. //if (!string.IsNullOrEmpty(res))
  134. //{
  135. // return res;
  136. //}
  137. //try
  138. //{
  139. // string url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=" + ip + "&resource_id=6006&ie=utf8&oe=gbk&format=json";
  140. // res = HttpMethods.HttpGet(url, Encoding.GetEncoding("GBK"));
  141. // var resjson = res.ToObject<obj>();
  142. // res = resjson.data[0].location;
  143. //}
  144. //catch
  145. //{
  146. // res = "";
  147. //}
  148. //return res;
  149. }
  150. /// <summary>
  151. /// 百度接口
  152. /// </summary>
  153. public class obj
  154. {
  155. public List<dataone> data { get; set; }
  156. }
  157. public class dataone
  158. {
  159. public string location { get; set; }
  160. }
  161. /// <summary>
  162. /// 聚合数据
  163. /// </summary>
  164. public class objex
  165. {
  166. public string resultcode { get; set; }
  167. public dataoneex result { get; set; }
  168. public string reason { get; set; }
  169. public string error_code { get; set; }
  170. }
  171. public class dataoneex
  172. {
  173. public string area { get; set; }
  174. public string location { get; set; }
  175. }
  176. #endregion
  177. #region Browser(获取浏览器信息)
  178. /// <summary>
  179. /// 获取浏览器信息
  180. /// </summary>
  181. public static string Browser
  182. {
  183. get
  184. {
  185. if (HttpContext.Current == null)
  186. return string.Empty;
  187. var browser = HttpContext.Current.Request.Browser;
  188. return string.Format("{0} {1}", browser.Browser, browser.Version);
  189. }
  190. }
  191. #endregion
  192. }
  193. }