在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
服务端: //方法一 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; }
来自印度的MCT Maulik Patel提供了一种服务端的解决方案,很好:
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. }
外加一种 最实用的:
这个要using System.Net; /// <summary> /// 获取ip地址 /// </summary> /// <returns></returns> private string GetIpAddress() { string ipAddress = "127.0.0.1"; IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName()); foreach (IPAddress ip in ips) { if (ip.AddressFamily.ToString().ToLower().Equals("internetwork")) { ipAddress = ip.ToString(); return ipAddress; } } return ipAddress; }
|
请发表评论