获取客户端IPv4的地址

哇,纠结了好几天,为了获取客户端IP地址,因为IPv6的出现好多问题,经常会碰到一些问题。

如:获取不到IP,有时候获取到 了却是IPv6的,你又未曾看到过,好吧,贴下代码

        /// <summary>
        /// 取得客戶端主机地址
        /// </summary>
        public static string GetClientIP()
        {
            if (null != HttpContext.Current.Request.ServerVariables["HTTP_VIA"])
            {
                return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            else
            {
                return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
        }

        /// <summary>
        /// 取得客戶端主机IPv4 地址
        /// </summary>
        /// <returns></returns>
        public static string GetClientIPv4()
        {
            string ipv4 = String.Empty;

            foreach (IPAddress ip in Dns.GetHostAddresses(GetClientIP()))
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    ipv4 = ip.ToString();
                    break;
                }
            }

            if (ipv4 != String.Empty)
            {
                return ipv4;
            }

            // 原代码使用 Dns.GetHostName 方法取回的是 Server 端资料,非 Client 端。
            // 改为利用 Dns.GetHostEntry 方法,由获取的 IPv6 位址反查 DNS 记录,
            // 再逐一判断是否属于 IPv4 协议定,如果是转换为 IPv4 地址。
            foreach (IPAddress ip in Dns.GetHostEntry(GetClientIP()).AddressList)
            //foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    ipv4 = ip.ToString();
                    break;
                }
            }

            return ipv4;
        }

 

 

展开阅读全文

没有更多推荐了,返回首页