获得代理IP或客户端Ip

public static string GetIP()
        {
            // 优先取得代理IP
            string userHostAddress = HttpContext.Current.Request.ServerVariables     

        ["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(userHostAddress))
            {
                //没有代理IP则直接取客户端IP
                userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if ((userHostAddress != null) && !(userHostAddress == string.Empty))
            {
                return userHostAddress;
            }
            return "0.0.0.0";
        }

posted @ 2011-02-22 20:36  双魂人生  阅读(420)  评论(0编辑  收藏  举报