韩言福地

只有想不到的,没有办不到的。 - HY Auspicious Place

« WebService获取服务端硬件信息和客户端IP,MAC,浏览器信息为什么会不快乐 »

获取客户端ip地址

      public string GetCustomerIP()
        {
            string CustomerIP="";
            if(HttpContext.Current.Request.ServerVariables["HTTP_VIA"]!=null)
            {
                CustomerIP=HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

            }
            else
            {
                CustomerIP=HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();


            }
            return CustomerIP;
       
                
        }

ServerVariables["HTTP_VIA"] 这个好象是判断是不是使用了代理.

这样可以获取到真实的ip地址.而不是代理ip

 

CustomerIP = HttpContext.Current.Request.UserHostAddress;

CustomerIP=HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();

这2句的话,应该是一样的效果,没有测试过.应该不会错的。

 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Copyright 2007-2010 www.yinrg.com(HY Auspicious Place) . 湘ICP备06007796号.