zoukankan      html  css  js  c++  java
  • 一个获取远程客户端真实IP的例子

    一个获取远程客户端真实IP的例子(仅供参考):

    View Code
     1 public static string GetIP()
     2     {
     3         string strIP = string.Empty;
     4         //获取代理用户真实IP地址
     5         //如果不是代理用户将返回null
     6         strIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
     7         if (string.IsNullOrEmpty(strIP))
     8         {
     9             //获取远程客户端请求的IP地址
    10             strIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    11         }
    12         if (string.IsNullOrEmpty(strIP))
    13         {
    14             //获取远程客户端的IP主机地址
    15             strIP = HttpContext.Current.Request.UserHostAddress;
    16         }
    17         if (string.IsNullOrEmpty(strIP) && !IsIP(strIP))
    18         {
    19             return "0.0.0.0";
    20         }
    21 
    22         return strIP;
    23     }
    24 
    25     public static bool IsIP(string strIP)
    26     {
    27         //验证IP格式
    28         return Regex.IsMatch(strIP, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
    29     }

    -------------------------------------------------------学习交流 欢迎指摘----------------------------------------------------------

  • 相关阅读:
    xshell使用密钥登陆服务器
    SQLyog使用隧道登陆数据库
    puttygen.exe生成ppk格式密钥
    xshell生成密钥对
    性能测试基础---jmeter函数二次开发
    Python:基础知识(二)
    异常点检测
    Python:numpy.newaxis
    三张图读懂机器学习:基本概念、五大流派与九种常见算法
    机器学习:样本集、验证集(开发集)、测试集
  • 原文地址:https://www.cnblogs.com/willpan/p/ServerVariables.html
Copyright © 2011-2022 走看看