(C# webservice 获取客户端IP疑难杂症)---试过n种方法都失败,获取代理服务器,访问者客户端真实IP

试过n种方法都失败,获取代理服务器,访问者客户端真实IP

不知是不是代理服务器设置什么或者是禁用什么,
因知道代理服务器的特殊性,如下介绍
clintA ---> proxy --->serverC 经过代理以后,由于在客户端和服务之间增加了中间层,因此服务器无法直接拿到客户端的IP,服务器端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息
请高手,指点一下我使用代码,为什么还是取不值客户端值.
(若代码没什么问题,请问大家有无遇到相同情况改什么系统配置..)
C# code
  public static string GetIP()
        {

            string result = String.Empty;

            result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }

            return result;



            /*这种都试过,还是不行.
           if (Context.Request.ServerVariables["HTTP_VIA"] != null) // 服务器, using proxy
            {
    
              ip = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
            }
            else//如果没有使用代理服务器或者得不到客户端的ip 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.
            }
            */

        }


我还试过循环打印出HttpContext.Current.Request.ServerVariables;与Request.Headers,所示key与value都找不到正确客户端IP

作者: learnJSee   发布时间: 2011-07-02

这问题问过无数次了...很多代理/网关不转发或不支持XFF,或者经过多层代理被过滤掉了...B/S是无解的...

作者: vrhero   发布时间: 2011-07-02

看看网上是不是有这样的一些免费的webservice的。可以用,但是这些都是别人的,也许那一天就不能用了。

作者: chen_ya_ping   发布时间: 2011-07-02

原文地址:https://www.cnblogs.com/guanshan/p/guan2020-9-19_001.html