zoukankan      html  css  js  c++  java
  • 获取客服端IP的完整解决方案

    来自印度的MCT Maulik Patel提供了一种服务端的获取IP解决方案

    if(Context.Request.ServerVariables["HTTP_VIA"]!=null// using proxy

         ip=Context.Request.ServerVariables[
    "HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP.
    }
    else// not using proxy or can't get the Client IP

         ip=Context.Request.ServerVariables[
    "REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
    }

     PS

    1. 有些代理是不会发给我们真实IP地址的

    2. 有些客户端会因为“header_access deny”的安全设置而不发给我们IP

    但是由于第二个备注在我们获取real client IP.出错(所以对上面的解决方案做如下调整)

     现在做如下解决方案

    /// <summary>

                            /// 获取远程IP

                            /// </summary>

                            /// <returns>远程主机的IP地址</returns>

                            public static string GetCustomerIP()

                            {

                                        string CustomerIP="";

                if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)// using proxy

                {

                    try

                    {

                        // Return real client IP.

                        CustomerIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

                    }

                    catch

                    {

                        //While it can't get the Client IP, it will return proxy IP.

                        CustomerIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();

                    }

                                        }

                                        else

                                        {

                    //While it can't get the Client IP, it will return proxy IP.

                                                    CustomerIP=HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();

                                        }

                                        return CustomerIP;

            }

  • 相关阅读:
    Anaconda+Tensorflow环境安装与配置
    计算机视觉(视频追踪检测分类、监控追踪)常用测试数据集
    迁移学习( Transfer Learning )
    matlab函数_连通区域
    GMM的EM算法实现
    对​O​p​e​n​C​V​直​方​图​的​数​据​结​构​C​v​H​i​s​t​o​g​r​a​m​的​理​解
    opencv基于混合高斯模型的图像分割
    LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
    视频测试序列(转)
    高职扩招,拿大专学历
  • 原文地址:https://www.cnblogs.com/huangbaixun/p/1293114.html
Copyright © 2011-2022 走看看