Asp.net access to the client browser to access the IP address instance code

Time:2021-6-7

This paper introduces the asp.net access to the client browser to access the IP address of the example code, to share with you, I also leave a note

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 ('Ip address: '+ returncitysn ["CIP"] +', City Code: '+ returncitysn ["CID"] +', region: '+ returncitysn ["CNAME"]; 
  </script> 
</head> 
<body>  
</body> 
</html>

2. Background code implementation

#Region IP address restriction function 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 ("Beijing". Equals (addr [0]) | "Beijing". Equals (addr [1])) 
     { 
       flag = true; 
     } 
     return flag; 
   } 
 
 
   /// <summary> 
   ///Get IP address of remote access user 
   /// </summary> 
   ///< returns > return IP address 
   protected string GetLoginIp() 
   { 
     string loginip = ""; 
     //Request. ServerVariables ['] -- get the set of service variables  
     if (Request.ServerVariables["REMOTE_ ADDR"] !=  Null) // judge whether the IP address of the remote host sending the request is empty 
     { 
       //Gets the IP address of the remote host that made the request 
       loginip = Request.ServerVariables["REMOTE_ADDR"].ToString(); 
     } 
     //Determine whether the registered user uses the setting agent 
     else if (Request.ServerVariables["HTTP_VIA"] != null) 
     { 
       if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) 
       { 
         //Gets the IP address of the proxy server 
         loginip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); 
       } 
       else 
       { 
         //Get client IP 
         loginip = Request.UserHostAddress; 
       } 
     } 
     else 
     { 
       //Get client IP 
       loginip = Request.UserHostAddress; 
     } 
     return loginip; 
   } 
 
 
 
 
   /// <summary> 
   ///According to IP 
   /// </summary> 
   public string[] GetAddressByIp(string ip) 
   { 
     string PostUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + ip; 
     string res = GetDataByPost(PostUrl);// The data returned by this request is: res = 1t115.193.210.0t115.194.201.255t China t Zhejiang t Hangzhou t Telecom 
     string[] arr = getAreaInfoList(res); 
     return arr; 
   } 
 
 
   /// <summary> 
   ///Post request data 
   /// </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> 
   ///Process the required data 
   /// </summary> 
   /// <param name="ip"></param> 
   /// <returns></returns> 
   public static string[] getAreaInfoList(string ipData) 
   { 
     //1t115.193.210.0t115.194.201.255t China t Zhejiang t Hangzhou t Telecom 
     string[] areaArr = new string[10]; 
     string[] newAreaArr = new string[2]; 
     try 
     { 
       //Take the required data, here only take the provinces and cities 
       areaArr = ipData.Split('t'); 
       newAreaArr[0] = areaArr[4];// province 
       newAreaArr[1] = areaArr[5];// city 
     } 
     catch (Exception e) 
     { 
 
 
     } 
     return newAreaArr; 
   } 
 
 
   #endregion

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.