zoukankan      html  css  js  c++  java
  • 获取ip

     #region 获得客户端IP

            /// <summary>
            /// 获得客户端IP
            /// </summary>
            /// <returns></returns>
            public string GetClientIp()
            {
                var ip = "127.0.0.1";
                //穿过代理服务器取远程用户真实IP地址
                if (HttpContext.Current == null
                    || HttpContext.Current.Request == null
                    || HttpContext.Current.Request.ServerVariables == null)
                {
                }
                //CDN加速后取到的IP     
                else if (HttpContext.Current.Request.Headers["Cdn-Src-Ip"] != null)
                {
                    ip = HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
                }
                else if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                {
                    ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                }
                else if (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
                {
                    ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
                }
                else if (HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"] != null)
                {
                    ip = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"].ToString();
                }
                else if (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
                {
                    ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
                }
                else
                {
                    ip = HttpContext.Current.Request.UserHostAddress;
                }
                return ip;
            }

            #endregion 获得客户端IP

  • 相关阅读:
    day09页面的声明周期函数
    day8小程序的运行环境与基本架构
    day09小程序复习
    day08前后端交互
    day07获取图片
    day07获取用户地址信息
    MySQL 主从同步延迟的原因及解决办法
    升级Oracle 19c经验: TTS 在使用datapump导matadata时EXCLUDE=STATISTICS 不启作用
    12c,19c自动kill长时间未活动会话特性
    SuSE11单实例二进制安装MySQL5.7
  • 原文地址:https://www.cnblogs.com/sll-fuling/p/9754078.html
Copyright © 2011-2022 走看看