C#获取真实ip地址

public static string GetIP()
    {
        string ipv4 = String.Empty;
        foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                ipv4 = IPA.ToString();
                break;
            }
        }
        if (ipv4 != String.Empty)
        {
            return ipv4;
        }
        foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                ipv4 = IPA.ToString();
                break;
            }
        }
        return ipv4;
    }





你可能感兴趣的:(.net开发)