服务端:
1 //方法一 2 HttpContext.Current.Request.UserHostAddress; 3 4 //方法二 5 HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 6 7 //方法三 8 string strHostName = System.Net.Dns.GetHostName(); 9 string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString(); 10 11 //方法四(无视代理) 12 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 //方法六(无视代理) 8 function GetLocalIPAddress() 9 { 10 var obj = null; 11 var rslt = ""; 12 try 13 { 14 obj = new ActiveXObject("rcbdyctl.Setting"); 15 rslt = obj.GetIPAddress; 16 obj = null; 17 } 18 catch(e) 19 { 20 // 21 } 22 return rslt; 23 }
来自印度的MCT Maulik Patel提供的一种服务端解决方案:
1 if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy 2 { 3 ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP. 4 } 5 else// not using proxy or can't get the Client IP 6 { 7 ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP. 9 }
备注: