欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-02-15 18:42:48
本文介绍了asp.net 获取客户端浏览器访问的ip地址的实例代码,分享给大家,自己也留个笔记 1、js方法 ...

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

1、js方法

<!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地址:' + returncitysn["cip"] + ', 城市编码:' + returncitysn["cid"] + ', 地区:' + returncitysn["cname"]); 
  </script> 
</head> 
<body>  
</body> 
</html> 

2、后台代码实现

#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('t'); 
       newareaarr[0] = areaarr[4];//省 
       newareaarr[1] = areaarr[5];//市 
     } 
     catch (exception e) 
     { 
 
 
     } 
     return newareaarr; 
   } 
 
 
   #endregion 

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