获取IP地址

时间:2009-07-21 05:48:53

标签: asp.net html

如何在文本框中获取并显示当前查看器的IP地址?

2 个答案:

答案 0 :(得分:6)

您可以使用HttpRequest.UserHostAddress属性或HttpRequest.ServerVariables集合的REMOTE_ADDR键,它们都是string,您只需将其值分配给TextBox:

HttpContext.Current.Request.UserHostAddress;

HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

答案 1 :(得分:0)

/// <summary>
/// IP Adress of computer
/// </summary>
/// <returns>return a string value(Ip address)</returns>
public static string IPaddress()
{
    String strHostName = "";
    strHostName = Dns.GetHostName();
    IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
    IPAddress[] addr = ipEntry.AddressList;
    string IPaddress = "";
    for (int i = 0; i < addr.Length; i++)
    {
        IPaddress = addr[i].ToString().Trim();
    }
    return IPaddress;
}