IpLocationHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace YiSha.Util
  7. {
  8. public class IpLocationHelper
  9. {
  10. #region IP位置查询
  11. public static string GetIpLocation(string ipAddress)
  12. {
  13. string ipLocation = string.Empty;
  14. try
  15. {
  16. if (!IsInnerIP(ipAddress))
  17. {
  18. ipLocation = GetIpLocationFromTaoBao(ipAddress);
  19. if (string.IsNullOrEmpty(ipLocation))
  20. {
  21. ipLocation = GetIpLocationFromPCOnline(ipAddress);
  22. }
  23. }
  24. }
  25. catch (Exception ex)
  26. {
  27. LogHelper.Error(ex);
  28. }
  29. return ipLocation;
  30. }
  31. private static string GetIpLocationFromTaoBao(string ipAddress)
  32. {
  33. string url = "http://ip.taobao.com/service/getIpInfo2.php";
  34. string postData = string.Format("ip={0}", ipAddress);
  35. string result = HttpHelper.HttpPost(url, postData);
  36. string ipLocation = string.Empty;
  37. if (!string.IsNullOrEmpty(result))
  38. {
  39. var json = JsonHelper.ToJObject(result);
  40. var jsonData = json["data"];
  41. if (jsonData != null)
  42. {
  43. ipLocation = jsonData["region"] + " " + jsonData["city"];
  44. ipLocation = ipLocation.Trim();
  45. }
  46. }
  47. return ipLocation;
  48. }
  49. private static string GetIpLocationFromPCOnline(string ipAddress)
  50. {
  51. HttpResult httpResult = new HttpHelper().GetHtml(new HttpItem
  52. {
  53. URL = "http://whois.pconline.com.cn/ip.jsp?ip=" + ipAddress,
  54. ContentType = "text/html; charset=gb2312"
  55. });
  56. string ipLocation = string.Empty;
  57. if (!string.IsNullOrEmpty(httpResult.Html))
  58. {
  59. var resultArr = httpResult.Html.Split(' ');
  60. ipLocation = resultArr[0].Replace("省", " ").Replace("市", "");
  61. ipLocation = ipLocation.Trim();
  62. }
  63. return ipLocation;
  64. }
  65. #endregion
  66. #region 判断是否是外网IP
  67. public static bool IsInnerIP(string ipAddress)
  68. {
  69. bool isInnerIp = false;
  70. long ipNum = GetIpNum(ipAddress);
  71. /**
  72. 私有IP:A类 10.0.0.0-10.255.255.255
  73. B类 172.16.0.0-172.31.255.255
  74. C类 192.168.0.0-192.168.255.255
  75. 当然,还有127这个网段是环回地址
  76. **/
  77. long aBegin = GetIpNum("10.0.0.0");
  78. long aEnd = GetIpNum("10.255.255.255");
  79. long bBegin = GetIpNum("172.16.0.0");
  80. long bEnd = GetIpNum("172.31.255.255");
  81. long cBegin = GetIpNum("192.168.0.0");
  82. long cEnd = GetIpNum("192.168.255.255");
  83. isInnerIp = IsInner(ipNum, aBegin, aEnd) || IsInner(ipNum, bBegin, bEnd) || IsInner(ipNum, cBegin, cEnd) || ipAddress.Equals("127.0.0.1");
  84. return isInnerIp;
  85. }
  86. /// <summary>
  87. /// 把IP地址转换为Long型数字
  88. /// </summary>
  89. /// <param name="ipAddress">IP地址字符串</param>
  90. /// <returns></returns>
  91. private static long GetIpNum(string ipAddress)
  92. {
  93. string[] ip = ipAddress.Split('.');
  94. long a = int.Parse(ip[0]);
  95. long b = int.Parse(ip[1]);
  96. long c = int.Parse(ip[2]);
  97. long d = int.Parse(ip[3]);
  98. long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
  99. return ipNum;
  100. }
  101. private static bool IsInner(long userIp, long begin, long end)
  102. {
  103. return (userIp >= begin) && (userIp <= end);
  104. }
  105. #endregion
  106. }
  107. }