zoukankan      html  css  js  c++  java
  • c# 获取客户端ip

    针对 HttpContext.Current.Request.UserHostAddress 如果客户使用的是代理,将获取不到真是的ip

    要想透过代理服务器取得客户端的真实IP地址,就要使用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取。 

    不过要注意的事,并不是每个代理服务器都能用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取客户端的真实 IP,有些用此方法读取到的仍然是代理服务器的IP。 

    还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 Request.ServerVariables ("HTTP_X_FORWARDED_FOR") 取到的值将是空的。因此,如果要在程序中使用此方法,可以这样处理: 

    string strIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (string.IsNullOrEmpty(strIp))
    {
    strIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }
    if (strIp.IndexOf(',') > 1)
    {
    strIp = strIp.Substring(0, strIp.IndexOf(','));
    }

  • 相关阅读:
    容器小结
    STL之Map和multimap容器
    STL之Set和multiset容器
    STL之优先级队列priority_queue
    STL之List容器
    STL之Queue容器
    STL之stack容器
    应用安全-提权/降权相关整理
    安卓监听工具整理
    Linux命令整理-Kali
  • 原文地址:https://www.cnblogs.com/ghelement/p/4522413.html
Copyright © 2011-2022 走看看