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;

            }

  • 相关阅读:
    在Servlet中使用JSON
    Servlet中Web.xml的配置详解
    项目人力资源管理的思考
    CRLF和LF
    Linux 时区变化
    开始 space viking 之旅
    HTML的标签canvas
    问题解决了——在虚拟机上测试串口软件 您会收到错误数据
    Very Deep Convolutional Networks for Large-Scale Image Recognition
    MapReduce的InputFormat学习过程
  • 原文地址:https://www.cnblogs.com/huangbaixun/p/1293114.html
Copyright © 2011-2022 走看看