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;

            }

  • 相关阅读:
    Struts2获取参数的几种方式
    Struts2的Action中访问servletAPI方式
    struts2中常用配置
    struts2发送ajax的几个问题(不使用struts2-json-plugin的情况下)
    深入Struts2的过滤器FilterDispatcher--中文乱码及字符编码过滤器
    Ironic 裸金属实例的部署流程
    Ironic 裸金属管理服务的底层技术支撑
    Cinder AZ 与 Nova AZ 的同步问题
    OpenStack 对接 Ceph 环境可以创建卷但不能挂载卷的问题
    OpenStack 节点重启后无法联网的问题
  • 原文地址:https://www.cnblogs.com/huangbaixun/p/1293114.html
Copyright © 2011-2022 走看看