簡體   English   中英

從主機名獲取 IPAddress

[英]Get IPAddress from a hostname

我想使用C#在我的aspx頁面上獲取主機的 IP 地址,我正在使用 DNS ZA2F2ED4F8DC40AB66 方法獲取這些

它在本地運行良好,但是當我在 IIS7 上部署解決方案時,它僅返回 ISP 分配的 IP 地址,但我想要該機器的本地 IP 地址。

有什么建議嗎?

這是示例。 在此示例中,我們可以獲得給定主機名的 IP 地址。

    string strHostName = "www.microsoft.com";
    // Get DNS entry of specified host name
    IPAddress[] addresses = Dns.GetHostEntry(strHostName).AddressList;

    // The DNS entry may contains more than one IP addresses.
    // Iterate them and display each along with the type of address (AddressFamily).
    foreach (IPAddress address in addresses)
    {
        Response.Write(string.Format("{0} = {1} ({2})", strHostName, address, address.AddressFamily));
        Response.Write("<br/><br/>");
    }

我相當有信心您無法像這樣獲取本地機器的本地 192.168.C.D 地址。

這是因為安全性和可見性(NAT 等)。

如果您正在尋找唯一標識用戶。 我會看會議或 cookies。

在公共 DNS 中查找 ip 地址時,您將獲得對外通信的官方 IP 地址。 如果使用 NAT 並且您想要內部地址,則必須連接到 DNS 服務器,該服務器包含內部 IP 地址。

你可以用這個方法...

public static String GetIP()
{
    String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

   if(string.IsNullOrEmpty(ip))
    {
        ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }

    return ip;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM