在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原文转自:http://www.cnblogs.com/blodfox777/archive/2008/07/21/1247447.html
服务端: 1 //方法一 2 3 HttpContext.Current.Request.UserHostAddress; 4 5 //方法二 6 HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 7 8 //方法三 9 string strHostName = System.Net.Dns.GetHostName(); 10 11 string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); 12 13 //方法四(无视代理) 14 HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
客户端: 1 //方法五 2 var ip = '<!--#echo var="REMOTE_ADDR"-->'; 3 alert("Your IP address is "+ip); 4 5 6 //方法六(无视代理) 7 function GetLocalIPAddress() 8 { 9 var obj = null; 10 var rslt = ""; 11 try 12 { 13 obj = new ActiveXObject("rcbdyctl.Setting"); 14 rslt = obj.GetIPAddress; 15 obj = null; 16 } 17 18 catch(e) 19 { 20 } 21 22 return rslt; 23 } 另一种服务端的解决方案: 1 if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy 2 { ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP. 3 } 4 else// not using proxy or can't get the Client IP 5 { 6 ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP. 7 }
备注:
1. 有些代理是不会发给我们真实IP地址的 2. 有些客户端会因为“header_access deny”的安全设置而不发给我们IP |
请发表评论