• 获取客户端IP


    #region 获取 客户端IP
          
          /// <summary>
          /// 获取客户端真实IP 
          /// </summary>
          /// <returns></returns>
          public static string GetClientRealIP()
            {
                string ip;
                try
                {
                    HttpRequest request = HttpContext.Current.Request;
    
                    if (request.ServerVariables["HTTP_VIA"] != null)
                    {
                        ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
                    }
                    else
                    {
                        ip = request.UserHostAddress;
                    }
                }
                catch (Exception e)
                {
                    //throw e;
                    //如果真实IP获取不到
                    ip = GetClientViaIP(); 
                }
    
                return ip;
            }
    
          /// <summary>
          /// 获取客户端IP 
          /// </summary>
          /// <returns></returns>
          public static string GetClientViaIP()
            {
                string viaIp = null;
    
                try
                {
                    HttpRequest request = HttpContext.Current.Request;
    
                    if (request.ServerVariables["HTTP_VIA"] != null)
                    {
                        viaIp = request.UserHostAddress;
                    }
    
                }
                catch (Exception e)
                {
                    //throw e;
                   viaIp= "000.000.0.0";
                }
    
                return viaIp;
            }
    
          #endregion
    
          public static string GetClientHostName()
          {
              string str = null;
              try
              {
                  HttpRequest request = HttpContext.Current.Request;
                  str = request.ServerVariables["REMOTE_HOST"];
              }
              catch (Exception)
              {
    
                  str = null;
              }
    
              return str;
          }
  • 相关阅读:
    OAuth
    PHP获取客户端的真实IP
    负载均衡----实现配置篇(Nginx)
    在线时间戳转换
    使用curl进行模拟登录
    定时任务
    Matplotlib使用教程
    CentOS7.X安装PHP
    Python虚拟环境的搭建与使用
    CentOS7.X安装openssl
  • 原文地址:https://www.cnblogs.com/New-world/p/3166608.html
Copyright © 2020-2023  润新知