查看: 2077|回复: 0

[.NET开发] asp.net 获取客户端浏览器访问的IP地址的实例代码

发表于 2018-1-2 08:00:00

本文介绍了asp.net 获取客户端浏览器访问的IP地址的实例代码,分享给大家,自己也留个笔记

1、js方法

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Document</title>
  6. <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
  7. <script type="text/javascript">
  8. document.write('IP地址:' + returnCitySN["cip"] + ', 城市编码:' + returnCitySN["cid"] + ', 地区:' + returnCitySN["cname"]);
  9. </script>
  10. </head>
  11. <body>
  12. </body>
  13. </html>
复制代码

2、后台代码实现

  1. #region IP地址限制功能 2017-07-18
  2. /// <summary>
  3. ///
  4. /// </summary>
  5. /// <returns></returns>
  6. public bool IsIPValidate()
  7. {
  8. bool flag = false;
  9. string userip = GetLoginIp();
  10. string[] addr = GetAddressByIp(userip);
  11. string addrs = addr[0] + addr[1];
  12. if ("北京".Equals(addr[0]) || "北京".Equals(addr[1]))
  13. {
  14. flag = true;
  15. }
  16. return flag;
  17. }
  18. /// <summary>
  19. /// 获取远程访问用户的Ip地址
  20. /// </summary>
  21. /// <returns>返回Ip地址</returns>
  22. protected string GetLoginIp()
  23. {
  24. string loginip = "";
  25. //Request.ServerVariables[""]--获取服务变量集合
  26. if (Request.ServerVariables["REMOTE_ADDR"] != null) //判断发出请求的远程主机的ip地址是否为空
  27. {
  28. //获取发出请求的远程主机的Ip地址
  29. loginip = Request.ServerVariables["REMOTE_ADDR"].ToString();
  30. }
  31. //判断登记用户是否使用设置代理
  32. else if (Request.ServerVariables["HTTP_VIA"] != null)
  33. {
  34. if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
  35. {
  36. //获取代理的服务器Ip地址
  37. loginip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
  38. }
  39. else
  40. {
  41. //获取客户端IP
  42. loginip = Request.UserHostAddress;
  43. }
  44. }
  45. else
  46. {
  47. //获取客户端IP
  48. loginip = Request.UserHostAddress;
  49. }
  50. return loginip;
  51. }
  52. /// <summary>
  53. /// 根据IP获取省市
  54. /// </summary>
  55. public string[] GetAddressByIp(string ip)
  56. {
  57. string PostUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + ip;
  58. string res = GetDataByPost(PostUrl);//该条请求返回的数据为:res=1t115.193.210.0t115.194.201.255t中国t浙江t杭州t电信
  59. string[] arr = getAreaInfoList(res);
  60. return arr;
  61. }
  62. /// <summary>
  63. /// Post请求数据
  64. /// </summary>
  65. /// <param name="url"></param>
  66. /// <returns></returns>
  67. public string GetDataByPost(string url)
  68. {
  69. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  70. string s = "anything";
  71. byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(s);
  72. req.Method = "POST";
  73. req.ContentType = "application/x-www-form-urlencoded";
  74. req.ContentLength = requestBytes.Length;
  75. Stream requestStream = req.GetRequestStream();
  76. requestStream.Write(requestBytes, 0, requestBytes.Length);
  77. requestStream.Close();
  78. HttpWebResponse res = (HttpWebResponse)req.GetResponse();
  79. StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
  80. string backstr = sr.ReadToEnd();
  81. sr.Close();
  82. res.Close();
  83. return backstr;
  84. }
  85. /// <summary>
  86. /// 处理所要的数据
  87. /// </summary>
  88. /// <param name="ip"></param>
  89. /// <returns></returns>
  90. public static string[] getAreaInfoList(string ipData)
  91. {
  92. //1t115.193.210.0t115.194.201.255t中国t浙江t杭州t电信
  93. string[] areaArr = new string[10];
  94. string[] newAreaArr = new string[2];
  95. try
  96. {
  97. //取所要的数据,这里只取省市
  98. areaArr = ipData.Split('t');
  99. newAreaArr[0] = areaArr[4];//省
  100. newAreaArr[1] = areaArr[5];//市
  101. }
  102. catch (Exception e)
  103. {
  104. }
  105. return newAreaArr;
  106. }
  107. #endregion
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报