获取真实的ip地址

展开阅读全文

asp.net如何获取客户端真实IP地址?

07-10

我试过了 以下的都不行rn都取到的是10开头的rnrn[code=C#]rnpublic static string IPAddressrn rn getrn rn string result = String.Empty;rnrn result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];rn if (result != null && result != String.Empty)rn rn //可能有代理 rn if (result.IndexOf(".") == -1) //没有“.”肯定是非IPv4格式 rn result = null;rn elsern rn if (result.IndexOf(",") != -1)rn rn //有“,”,估计多个代理。取第一个不是内网的IP。 rn result = result.Replace(" ", "").Replace("'", "");rn string[] temparyip = result.Split(",;".ToCharArray());rn for (int i = 0; i < temparyip.Length; i++)rn rn if (IsIPAddress(temparyip[i])rn && temparyip[i].Substring(0, 3) != "10."rn && temparyip[i].Substring(0, 7) != "192.168"rn && temparyip[i].Substring(0, 7) != "172.16.")rn rn return temparyip[i]; //找到不是内网的地址 rn rn rn rn else if (IsIPAddress(result)) //代理即是IP格式 rn return result;rn elsern result = null; //代理中的内容 非IP,取IP rn rnrn rnrn string IpAddress = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty) ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];rnrnrnrn if (null == result || result == String.Empty)rn result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];rnrn if (result == null || result == String.Empty)rn result = HttpContext.Current.Request.UserHostAddress;rnrn return result;rn rnrnstring ip = System.Net.IPAddress.Parse(System.Web.HttpContext.Current.Request.UserHostAddress);rnrnrnSystem.Net.IPAddress ips = System.Net.IPAddress.Parse(System.Web.HttpContext.Current.Request.UserHostAddress);rn ip = ips.ToString();rnrnrn if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)rn ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();rn elsern ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();rnrnrn if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)rn rnrn ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();rnrn rn elsern rnrn ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();rnrn rn[/code] 论坛

没有更多推荐了,返回首页