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
  • 相关阅读:
    幂等性知识学习梳理
    使用Less,FontAwesome 重写EasyUI TreeGrid样式
    根据表达式树动态生成Lambda表达式
    elasticsearch-query-builder, 一款可以基于配置化以及参数绑定的ES语句构造神器
    hadoop2 5个环境配置文件
    hadoop3.0.0 分布式集群安装过程
    linux 常用命令
    securecrt 常用快捷键
    java 面试题
    Spark Streaming 读取 Kafka 数据的两种方式
  • 原文地址:https://www.cnblogs.com/jimcsharp/p/2733571.html
Copyright © 2011-2022 走看看