Dns.GetHostEntry 方法

定義

將主機名稱或 IP 位址解析至 IPHostEntry 執行個體。Resolves a host name or IP address to an IPHostEntry instance.

多載

GetHostEntry(IPAddress)

將 IP 位址解析至 IPHostEntry 執行個體。Resolves an IP address to an IPHostEntry instance.

GetHostEntry(String)

將主機名稱或 IP 位址解析至 IPHostEntry 執行個體。Resolves a host name or IP address to an IPHostEntry instance.

GetHostEntry(IPAddress)

將 IP 位址解析至 IPHostEntry 執行個體。Resolves an IP address to an IPHostEntry instance.

public:
 static System::Net::IPHostEntry ^ GetHostEntry(System::Net::IPAddress ^ address);
public static System.Net.IPHostEntry GetHostEntry (System.Net.IPAddress address);
static member GetHostEntry : System.Net.IPAddress -> System.Net.IPHostEntry
Public Shared Function GetHostEntry (address As IPAddress) As IPHostEntry

參數

address
IPAddress

IP 位址。An IP address.

傳回

IPHostEntry

IPHostEntry 執行個體,含有在 address 中所指定之主機的相關位址資訊。An IPHostEntry instance that contains address information about the host specified in address.

例外狀況

addressnulladdress is null.

當解析 address 時,發生錯誤。An error is encountered when resolving address.

address 為無效的 IP 位址。address is an invalid IP address.

範例

下列程式碼範例會使用 GetHostEntry 方法,將 IP 位址解析成 IPHostEntry 實例。The following code example uses the GetHostEntry method to resolve an IP address to an IPHostEntry instance.

public:
    static void DoGetHostEntry(IPAddress^ address)
    {
        IPHostEntry^ host = Dns::GetHostEntry(address);

        Console::WriteLine("GetHostEntry({0}) returns HostName: {1}", address->ToString(), host->HostName);
    }
public static void DoGetHostEntry(IPAddress address)
{
    IPHostEntry host = Dns.GetHostEntry(address);

    Console.WriteLine($"GetHostEntry({address}) returns HostName: {host.HostName}");
}
Public Sub DoGetHostEntry(address As IPAddress)

    Dim host as IPHostEntry = Dns.GetHostEntry(address)

    Console.WriteLine($"GetHostEntry({address}) returns HostName: {host.HostName}")

End Sub

備註

GetHostEntry 方法會查詢 DNS 伺服器,以取得與 IP 位址相關聯的 IP 位址和別名。The GetHostEntry method queries a DNS server for the IP addresses and aliases associated with an IP address.

如果本機電腦未安裝 IPv6,則會從 GetHostEntry 方法的結果篩選 IPv6 位址。IPv6 addresses are filtered from the results of the GetHostEntry method if the local computer does not have IPv6 installed. 因此,如果只有 IPv6 結果可供 address 參數使用,則可能會取回空的 IPHostEntry 實例。As a result, it is possible to get back an empty IPHostEntry instance if only IPv6 results were available for the address parameter.

傳回之 IPHostEntry 實例的 Aliases 屬性不會由這個方法填入,而且一律為空白。The Aliases property of the IPHostEntry instance returned is not populated by this method and will always be empty.

注意

當您在應用程式中啟用網路追蹤時,這個成員會發出追蹤資訊。This member emits trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

GetHostEntry(String)

將主機名稱或 IP 位址解析至 IPHostEntry 執行個體。Resolves a host name or IP address to an IPHostEntry instance.

public:
 static System::Net::IPHostEntry ^ GetHostEntry(System::String ^ hostNameOrAddress);
public static System.Net.IPHostEntry GetHostEntry (string hostNameOrAddress);
static member GetHostEntry : string -> System.Net.IPHostEntry
Public Shared Function GetHostEntry (hostNameOrAddress As String) As IPHostEntry

參數

hostNameOrAddress
String

要解析的主機名稱或 IP 位址。The host name or IP address to resolve.

傳回

IPHostEntry

IPHostEntry 執行個體,含有在 hostNameOrAddress 中所指定之主機的相關位址資訊。An IPHostEntry instance that contains address information about the host specified in hostNameOrAddress.

例外狀況

hostNameOrAddress 參數為 nullThe hostNameOrAddress parameter is null.

hostNameOrAddress 參數的長度大於 255 個字元。The length of hostNameOrAddress parameter is greater than 255 characters.

解析 hostNameOrAddress 參數時發生錯誤。An error was encountered when resolving the hostNameOrAddress parameter.

hostNameOrAddress 參數是無效的 IP 位址。The hostNameOrAddress parameter is an invalid IP address.

範例

下列範例會使用 GetHostEntry 方法,將 IP 位址解析成 IPHostEntry 實例。The following example uses the GetHostEntry method to resolve an IP address to an IPHostEntry instance.

public:
    static void DoGetHostEntry(String^ hostname)
    {
        IPHostEntry^ host = Dns::GetHostEntry(hostname);

        Console::WriteLine("GetHostEntry({0}) returns:", host->HostName);

        for (int i = 0; i < host->AddressList->Length; i++)
        {
            Console::WriteLine("    {0}", host->AddressList[i]->ToString());			
        }
    }
public static void DoGetHostEntry(string hostname)
{
    IPHostEntry host = Dns.GetHostEntry(hostname);

    Console.WriteLine($"GetHostEntry({hostname}) returns:");

    foreach (IPAddress address in host.AddressList)
    {
        Console.WriteLine($"    {address}");
    }
}
Public Sub DoGetHostEntry(hostName As String)

    Dim host as IPHostEntry = Dns.GetHostEntry(hostname)

    Console.WriteLine($"GetHostEntry({hostname}) returns:")

    Dim addresses As IPAddress() = host.AddressList

    Dim index As Integer
    For index = 0 To addresses.Length - 1
        Console.WriteLine($"    {addresses(index)}")
    Next index

End Sub

備註

GetHostEntry 方法會查詢 DNS 伺服器,以取得與主機名稱或 IP 位址相關聯的 IP 位址。The GetHostEntry method queries a DNS server for the IP address that is associated with a host name or IP address.

如果將空字串當做 hostNameOrAddress 引數傳遞,則此方法會傳回本機主機的 IPv4 和 IPv6 位址。If an empty string is passed as the hostNameOrAddress argument, then this method returns the IPv4 and IPv6 addresses of the local host.

如果找不到主機名稱,則會傳回值為11001的 SocketException 例外狀況(Windows Socket error WSAHOST_NOT_FOUND)。If the host name could not be found, the SocketException exception is returned with a value of 11001 (Windows Sockets error WSAHOST_NOT_FOUND). 如果 DNS 伺服器沒有回應,則會傳回此例外狀況。This exception can be returned if the DNS server does not respond. 如果名稱不是官方主機名稱或別名,或在查詢的資料庫中找不到,也可能會傳回此例外狀況。This exception can also be returned if the name is not an official host name or alias, or it cannot be found in the database(s) being queried.

如果 hostNameOrAddress 參數包含 AnyIPv6Any,也會傳回 ArgumentException 例外狀況。The ArgumentException exception is also returned if the hostNameOrAddress parameter contains Any or IPv6Any.

GetHostEntry 方法會假設,如果在 hostNameOrAddress 參數中傳遞 IP 常值字串,應用程式會想要使用所有屬性集傳回的 IPHostEntry 實例。The GetHostEntry method assumes that if an IP literal string is passed in the hostNameOrAddress parameter that the application wants an IPHostEntry instance returned with all of the properties set. 這些屬性包括 AddressListAliasesHostNameThese properties include the AddressList, Aliases, and HostName. 因此,在傳遞 IP 字串常值時,GetHostEntry 方法的執行會展示下列行為:As a result, the implementation of the GetHostEntry method exhibits the following behavior when an IP string literal is passed:

  1. 方法會嘗試剖析位址。The method tries to parse the address. 如果 hostNameOrAddress 參數包含合法的 IP 字串常值,則第一個階段會成功。If the hostNameOrAddress parameter contains a legal IP string literal, then the first phase succeeds.

  2. 使用 IP 字串常值的 IP 位址的反向對應嘗試取得主機名稱。A reverse lookup using the IP address of the IP string literal is attempted to obtain the host name. 此結果會設定為 HostName 屬性。This result is set as the HostName property.

  3. 此反向對應的主機名稱會再次使用,以取得與該名稱相關聯的所有可能 IP 位址,並將其設定為 AddressList 屬性。The host name from this reverse lookup is used again to obtain all the possible IP addresses associated with the name and set as the AddressList property.

對於 IPv4 字串常值,上述三個步驟可能會成功。For an IPv4 string literal, all three steps above may succeed. 但是,對於實際屬於不同主機的 IPv4 位址,可能會傳回過時的 DNS 記錄。But it is possible for a stale DNS record for an IPv4 address that actually belongs to a different host to be returned. 這可能會導致步驟 #3 失敗,並擲回例外狀況(IPv4 位址有 DNS PTR 記錄,但沒有 IPv4 位址的 DNS A 記錄)。This may cause step #3 to fail and throw an exception (there is a DNS PTR record for the IPv4 address, but no DNS A record for the IPv4 address).

若為 IPv6,上述步驟 #2 可能會失敗,因為大部分的 IPv6 部署都不會註冊 IPv6 位址的反向(PTR)記錄。For IPv6, step #2 above may fail, since most IPv6 deployments do not register the reverse (PTR) record for an IPv6 address. 因此,這個方法可能會傳回字串 IPv6 常值做為 HostName 屬性中的完整網域(FQDN)主機名稱。So this method may return the string IPv6 literal as the fully-qualified domain (FQDN) host name in the HostName property.

GetHostAddresses 方法與 IP 常值有不同的行為。The GetHostAddresses method has different behavior with respect to IP literals. 如果上述步驟 #1 成功(它成功剖析為 IP 位址),則會立即傳回該位址做為結果。If step #1 above succeeds (it successfully parses as an IP address), that address is immediately returned as the result. 反向對應不會嘗試。There is no attempt at a reverse lookup.

如果本機電腦未安裝 IPv6,則會從 GetHostEntry 方法的結果篩選 IPv6 位址。IPv6 addresses are filtered from the results of the GetHostEntry method if the local computer does not have IPv6 installed. 因此,如果只有適用于 hostNameOrAddress的 IPv6 結果,就可以取回空的 IPHostEntry 實例。As a result, it is possible to get back an empty IPHostEntry instance if only IPv6 results where available for the hostNameOrAddress.parameter.

傳回之 IPHostEntry 實例的 Aliases 屬性不會由這個方法填入,而且一律為空白。The Aliases property of the IPHostEntry instance returned is not populated by this method and will always be empty.

注意

當您在應用程式中啟用網路追蹤時,這個成員會發出追蹤資訊。This member emits trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

適用於