日期:2014-05-17  浏览次数:20548 次

服务端获取 用户IP,速战速结贴

     1.
             string strHostName = System.Net.Dns.GetHostName();
             string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
2.
            string s = Page.Request.UserHostAddress;


上面是网上找的方法,都不能获取到IP,到底如何获取,分不多,但真心求教
------最佳解决方案--------------------
 private static string get_IPv4(HttpRequest pRequest)
        {
            string sIpv4 = String.Empty;

            foreach (IPAddress ip in Dns.GetHostAddresses(pRequest.UserHostAddress))
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    sIpv4 = ip.ToString();
                    break;
                }
            }
            if (sIpv4 != String.Empty)
            {
                return sIpv4;
            }

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

            return sIpv4;
        }
get_IPv4(Page.Request);
------其他解决方案--------------------
引用:
想请教多下,如果是ipv6改怎么办

private static string get_IPv6(HttpRequest pRequest)
{
string sIpv6 = String.Empty;

//ipv6取得
sIpv6 = pRequest.UserHostAddress.ToString();

//ipv4が取得された場合は空白文字にする