Home > Backend Development > C#.Net Tutorial > How to get the IP address accessed by the browser in asp.net?

How to get the IP address accessed by the browser in asp.net?

黄舟
Release: 2017-07-20 15:53:58
Original
2066 people have browsed it

This article mainly introduces the example code of asp.net to obtain the IP address accessed by the client browser. It has certain reference value. Interested friends can refer to it.

This article introduces asp .net Get the example code of the IP address accessed by the client browser, share it with everyone, and leave a note for yourself

1, js method


##

<!DOCTYPE html> 
<html> 
<head> 
  <meta charset="UTF-8" /> 
  <title>Document</title> 
  <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>  
  <script type="text/javascript">  
    document.write(&#39;IP地址:&#39; + returnCitySN["cip"] + &#39;, 城市编码:&#39; + returnCitySN["cid"] + &#39;, 地区:&#39; + returnCitySN["cname"]); 
  </script> 
</head> 
<body>  
</body> 
</html>
Copy after login

2. Backend code implementation


#region IP地址限制功能 2017-07-18 
 
 
   /// <summary> 
   ///  
   /// </summary> 
   /// <returns></returns> 
   public bool IsIPValidate() 
   { 
     bool flag = false; 
     string userip = GetLoginIp(); 
     string[] addr = GetAddressByIp(userip); 
     string addrs = addr[0] + addr[1]; 
     if ("北京".Equals(addr[0]) || "北京".Equals(addr[1])) 
     { 
       flag = true; 
     } 
     return flag; 
   } 
 
 
   /// <summary> 
   /// 获取远程访问用户的Ip地址 
   /// </summary> 
   /// <returns>返回Ip地址</returns> 
   protected string GetLoginIp() 
   { 
     string loginip = ""; 
     //Request.ServerVariables[""]--获取服务变量集合  
     if (Request.ServerVariables["REMOTE_ADDR"] != null) //判断发出请求的远程主机的ip地址是否为空 
     { 
       //获取发出请求的远程主机的Ip地址 
       loginip = Request.ServerVariables["REMOTE_ADDR"].ToString(); 
     } 
     //判断登记用户是否使用设置代理 
     else if (Request.ServerVariables["HTTP_VIA"] != null) 
     { 
       if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) 
       { 
         //获取代理的服务器Ip地址 
         loginip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); 
       } 
       else 
       { 
         //获取客户端IP 
         loginip = Request.UserHostAddress; 
       } 
     } 
     else 
     { 
       //获取客户端IP 
       loginip = Request.UserHostAddress; 
     } 
     return loginip; 
   } 

   /// <summary> 
   /// 根据IP获取省市 
   /// </summary> 
   public string[] GetAddressByIp(string ip) 
   { 
     string PostUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + ip; 
     string res = GetDataByPost(PostUrl);//该条请求返回的数据为:res=1t115.193.210.0t115.194.201.255t中国t浙江t杭州t电信 
     string[] arr = getAreaInfoList(res); 
     return arr; 
   } 

   /// <summary> 
   /// Post请求数据 
   /// </summary> 
   /// <param name="url"></param> 
   /// <returns></returns> 
   public string GetDataByPost(string url) 
   { 
     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 
     string s = "anything"; 
     byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(s); 
     req.Method = "POST"; 
     req.ContentType = "application/x-www-form-urlencoded"; 
     req.ContentLength = requestBytes.Length; 
     Stream requestStream = req.GetRequestStream(); 
     requestStream.Write(requestBytes, 0, requestBytes.Length); 
     requestStream.Close(); 
 
 
     HttpWebResponse res = (HttpWebResponse)req.GetResponse(); 
     StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default); 
     string backstr = sr.ReadToEnd(); 
     sr.Close(); 
     res.Close(); 
     return backstr; 
   } 
 
 
   /// <summary> 
   /// 处理所要的数据 
   /// </summary> 
   /// <param name="ip"></param> 
   /// <returns></returns> 
   public static string[] getAreaInfoList(string ipData) 
   { 
     //1t115.193.210.0t115.194.201.255t中国t浙江t杭州t电信 
     string[] areaArr = new string[10]; 
     string[] newAreaArr = new string[2]; 
     try 
     { 
       //取所要的数据,这里只取省市 
       areaArr = ipData.Split(&#39;t&#39;); 
       newAreaArr[0] = areaArr[4];//省 
       newAreaArr[1] = areaArr[5];//市 
     } 
     catch (Exception e) 
     { 
 
 
     } 
     return newAreaArr; 
   } 
 
 
   #endregion
Copy after login

The above is the detailed content of How to get the IP address accessed by the browser in asp.net?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template