0

我正在创建肥皂消息,需要找到一个请求的客户端 IP。我一直在寻找一种方法来查找客户端 IP,但仍然无法。你能告诉我在收到客户端请求时如何找到客户端 IP 的步骤吗?

4

1 回答 1

-1

因此,您希望能够从服务内部查看使用 Web 服务的客户端的 IP 地址吗?

您可以创建一个这样的类来检查您的一些服务器变量,其中一些从 HTTP 标头中提取数据。我发现只需查看 HTTP_X_FORWARDED_FOR 和 REMOTE_ADDR 就可以解决问题(例如在 C# 中):

public class IP
{
    public static String UserHostAddress { get { return HttpContext.Current.Request.UserHostAddress; } }
    public static String REMOTE_ADDR { get { return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } }
    public static String HTTP_X_FORWARDED_FOR { get { return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; } }
    public static String HTTP_CLIENT_IP { get { return HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"]; } }
    public static String HTTP_X_FORWARDED { get { return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED"]; } }
    public static String HTTP_X_CLUSTER_CLIENT_IP { get { return HttpContext.Current.Request.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"]; } }
    public static String HTTP_FORWARDED_FOR { get { return HttpContext.Current.Request.ServerVariables["HTTP_FORWARDED_FOR"]; } }
    public static String HTTP_FORWARDED { get { return HttpContext.Current.Request.ServerVariables["HTTP_FORWARDED"]; } }

    public static String IP_ADDRESS
    {
        get
        {
            if (HTTP_X_FORWARDED_FOR != null)
            {
                return HTTP_X_FORWARDED_FOR;
            }

            return REMOTE_ADDR;
        }
    }
}
于 2013-04-09T00:28:48.070 回答