C# 获取客户端IP

    public static string GetRealIP()
    {
        string ip;
        try
        {
            HttpRequest request = HttpContext.Current.Request;

            if (request.ServerVariables["HTTP_VIA"] != null)
            {
                ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
            }
            else
            {
                ip = request.UserHostAddress;
            }
        }
        catch (Exception e)
        {
            throw e;
        }

        return ip;
    }

    public static string GetViaIP()
    {
        string viaIp = null;

        try
        {
            HttpRequest request = HttpContext.Current.Request;

            if (request.ServerVariables["HTTP_VIA"] != null)
            {
                viaIp = request.UserHostAddress;
            }

        }
        catch (Exception e)
        {

            throw e;
        }

        return viaIp;
    }

posted on 2009-07-11 10:44  猎风  阅读(2332)  评论(1)    收藏  举报