博主喝口茶,一毛也是爱

收缩

C# 获取接口请求IP地址

165 人阅读
分类:
public static class IPHelper
{
    public static string GetIpAddress()
    {
        var ipAddress = "0.0.0.0";

        try
        {
            if (HttpContext.Current == null
            || HttpContext.Current.Request == null
            || HttpContext.Current.Request.ServerVariables == null)
            {
                ipAddress = "0.0.0.2";
                return ipAddress;
            }

            //CDN加速后取到的IP simone 090805
            ipAddress = HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
            if (!string.IsNullOrEmpty(ipAddress))
            {
                ipAddress = "0.0.0.3";
                return ipAddress;
            }

            ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (!String.IsNullOrEmpty(ipAddress))
            {
                ipAddress = "0.0.0.4";
                return ipAddress;
            }

            if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (ipAddress == null)
                    ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            else
            {
                ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

            }

            if (string.Compare(ipAddress, "unknown", true) == 0)
            {
                ipAddress = HttpContext.Current.Request.UserHostAddress;
            }
        }
        catch
        {
            ipAddress = "0.0.0.5";
        }

        return ipAddress;
    }
}


和博主交个朋友吧
    发布篇幅
    • 文章总数:0
    • 原创:0
    • 转载:0
    • 译文:0
    文章分类
      文章存档
      阅读排行