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

     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;
    
            }
    

      

  • 相关阅读:
    C语言点滴
    随便记点什么
    STL的使用
    Linux下OTG支持USB摄像头
    socket编程实战-调试
    socket编程实战-bind端口占用问题
    socket编程实战-tcp_tw_recycle问题
    socket编程实战-connect超时问题
    BT[3]-BLE广播详解
    BT[2]-BLE初体验:心率计
  • 原文地址:https://www.cnblogs.com/xffy1028/p/3935764.html
Copyright © 2011-2022 走看看