首页 > 代码库 > 获取IP地址

获取IP地址

//方法一HttpContext.Current.Request.UserHostAddress; //方法二HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];//方法三string strHostName = System.Net.Dns.GetHostName();string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();//方法四(无视代理)HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
服务端:
//方法五var ip = <!--#echo var="REMOTE_ADDR"-->;alert("Your IP address is "+ip);//方法六(无视代理)function GetLocalIPAddress() {     var obj = null;     var rslt = "";     try     {         obj = new ActiveXObject("rcbdyctl.Setting");         rslt = obj.GetIPAddress;         obj = null;     }     catch(e)     {         //     }    return rslt; }
客户端:
if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy{      ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP.}else// not using proxy or can‘t get the Client IP{      ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can‘t get the Client IP, it will return proxy IP.}
种服务端解决方案

 

获取IP地址