NetHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using System.Web;
  10. namespace Common
  11. {
  12. public sealed class NetHelper
  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 (!string.IsNullOrEmpty(result))
  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. {
  40. return hostAddress.ToString();
  41. }
  42. else
  43. {
  44. return ip == "::1" ? "127.0.0.0" : ip;
  45. }
  46. }
  47. return string.Empty;
  48. }
  49. /// <summary>
  50. /// 获取Web远程Ip
  51. /// </summary>
  52. public static string GetWebRemoteIp()
  53. {
  54. return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  55. }
  56. /// <summary>
  57. /// 获取局域网IP
  58. /// </summary>
  59. private static string GetLanIp()
  60. {
  61. foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
  62. {
  63. if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
  64. return hostAddress.ToString();
  65. }
  66. return string.Empty;
  67. }
  68. #endregion Ip(获取Ip)
  69. #region Host(获取主机名)
  70. /// <summary>
  71. /// 获取主机名
  72. /// </summary>
  73. public static string Host
  74. {
  75. get
  76. {
  77. return HttpContext.Current == null ? Dns.GetHostName() : GetWebClientHostName();
  78. }
  79. }
  80. /// <summary>
  81. /// 获取Web客户端主机名
  82. /// </summary>
  83. private static string GetWebClientHostName()
  84. {
  85. if (!HttpContext.Current.Request.IsLocal)
  86. return string.Empty;
  87. var ip = GetWebRemoteIp();
  88. var result = Dns.GetHostEntry(IPAddress.Parse(ip)).HostName;
  89. if (result == "localhost.localdomain")
  90. result = Dns.GetHostName();
  91. return result;
  92. }
  93. #endregion Host(获取主机名)
  94. #region Browser(获取浏览器信息)
  95. /// <summary>
  96. /// 获取浏览器信息
  97. /// </summary>
  98. public static string Browser
  99. {
  100. get
  101. {
  102. if (HttpContext.Current == null)
  103. return string.Empty;
  104. var browser = HttpContext.Current.Request.Browser;
  105. return string.Format("{0} {1}", browser.Browser, browser.Version);
  106. }
  107. }
  108. #endregion Browser(获取浏览器信息)
  109. #region 通过IP得到IP所在地省市
  110. /// <summary>
  111. /// 将Unicode编码转换成中文
  112. /// </summary>
  113. /// <param name="result"></param>
  114. /// <returns></returns>
  115. private static string ConvertUnicode2Chinese(string result)
  116. {
  117. Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
  118. return reg.Replace(result, delegate (Match m)
  119. {
  120. return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString();
  121. });
  122. }
  123. private static bool HasContains(Predicate<string> handler, string str)
  124. {
  125. return handler.Invoke(str);
  126. }
  127. #endregion 通过IP得到IP所在地省市
  128. #region 其他
  129. /// <summary>
  130. /// IP地址信息
  131. /// </summary>
  132. private class IPAddressItem
  133. {
  134. public int ret { get; set; }
  135. public string start { get; set; }
  136. public string end { get; set; }
  137. public string country { get; set; }
  138. public string province { get; set; }
  139. public string city { get; set; }
  140. public string district { get; set; }
  141. public string isp { get; set; }
  142. public string type { get; set; }
  143. public string desc { get; set; }
  144. }
  145. #endregion 其他
  146. }
  147. }