• 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
  • 相关阅读:
    人脸识别常用数据集大全(12/20更新)
    103 保序回归 isotonic regression
    SVM的概率输出(Platt scaling)
    scikit_learn 中文说明入门
    外点惩处函数法·约束优化问题
    unigui 设置单元格颜色
    一些仪器的解码程序(delphi)
    phpstudy 配置 memcached / memcache
    OmniThreadLibrary学习笔记
    注意微信支付的配置
  • 原文地址:https://www.cnblogs.com/jimcsharp/p/2733571.html
Copyright © 2020-2023  润新知