zoukankan      html  css  js  c++  java
  • C# Web 获取客户端IP

    #region 获取web客户端ip

            /// <summary>
            /// 获取web客户端ip
            /// </summary>
            /// <returns></returns>
            public static string GetWebClientIp()
            {
             
                    string userIP = "未获取用户IP";
     
                    try
                    {
                        if (System.Web.HttpContext.Current == null
                    || System.Web.HttpContext.Current.Request == null
                    || System.Web.HttpContext.Current.Request.ServerVariables == null)
                            return "";
     
                        string CustomerIP = "";
     
                        //CDN加速后取到的IP simone 090805
                        CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
                        if (!string.IsNullOrEmpty(CustomerIP))
                        {
                            return CustomerIP;
                        }
     
                        CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
     
                       
                        if (!String.IsNullOrEmpty(CustomerIP))
                            return CustomerIP;
     
                        if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                        {
                            CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                            if (CustomerIP == null)
                                CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                        }
                        else
                        {
                            CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
     
                        }
     
                        if (string.Compare(CustomerIP, "unknown", true) == 0)
                            return System.Web.HttpContext.Current.Request.UserHostAddress;
                        return CustomerIP;
                    }
                    catch { }
     
                    return userIP;
               
            }
            #endregion
  • 相关阅读:
    HTML语义化之常见模块
    取当前时间,格式为,yyyy-mm-dd hh:mm:ss
    利用JS 在网页上获取并显示当前日期 星期
    Javascript Math ceil()、floor()、round()三个函数的区别
    JS,JQUERY 常用笔记
    适配不同分辨率屏幕
    选取节点常用方法
    js控制使div自动适应居中
    点击jQuery Mobile的按钮改变颜色
    左侧固定宽度 右侧自适应
  • 原文地址:https://www.cnblogs.com/jimcsharp/p/2733571.html
Copyright © 2011-2022 走看看