Dns.GetHostByName(String) 方法

定義

警告

GetHostByName is obsoleted for this type, please use GetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202

警告

GetHostByName has been deprecated. Use GetHostEntry instead.

警告

GetHostByName is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202

取得指定 DNS 主機名稱的 DNS 資訊。

public:
 static System::Net::IPHostEntry ^ GetHostByName(System::String ^ hostName);
[System.Obsolete("GetHostByName is obsoleted for this type, please use GetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")]
public static System.Net.IPHostEntry GetHostByName (string hostName);
[System.Obsolete("GetHostByName has been deprecated. Use GetHostEntry instead.")]
public static System.Net.IPHostEntry GetHostByName (string hostName);
[System.Obsolete("GetHostByName is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")]
public static System.Net.IPHostEntry GetHostByName (string hostName);
public static System.Net.IPHostEntry GetHostByName (string hostName);
[<System.Obsolete("GetHostByName is obsoleted for this type, please use GetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")>]
static member GetHostByName : string -> System.Net.IPHostEntry
[<System.Obsolete("GetHostByName has been deprecated. Use GetHostEntry instead.")>]
static member GetHostByName : string -> System.Net.IPHostEntry
[<System.Obsolete("GetHostByName is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")>]
static member GetHostByName : string -> System.Net.IPHostEntry
static member GetHostByName : string -> System.Net.IPHostEntry
Public Shared Function GetHostByName (hostName As String) As IPHostEntry

參數

hostName
String

主機的 DNS 名稱。

傳回

IPHostEntry 物件,含有在 hostName 中所指定之位址的主機資訊。

屬性

例外狀況

hostNamenull

hostName 的長度大於 255 個字元。

當解析 hostName 時,發生錯誤。

範例

下列範例會 GetHostByName 使用 方法來取得指定 DNS 主機名的 DNS 資訊。

try
{
   IPHostEntry^ hostInfo = Dns::GetHostByName( hostName );
   
   // Get the IP address list that resolves to the host names contained in the
   // Alias property.
   array<IPAddress^>^address = hostInfo->AddressList;
   
   // Get the alias names of the addresses in the IP address list.
   array<String^>^alias = hostInfo->Aliases;
   Console::WriteLine( "Host name : {0}", hostInfo->HostName );
   Console::WriteLine( "\nAliases : " );
   for ( int index = 0; index < alias->Length; index++ )
      Console::WriteLine( alias[ index ] );
   Console::WriteLine( "\nIP address list : " );
   for ( int index = 0; index < address->Length; index++ )
      Console::WriteLine( address[ index ] );
}
catch ( SocketException^ e ) 
{
   Console::WriteLine( "SocketException caught!!!" );
   Console::WriteLine( "Source : {0}", e->Source );
   Console::WriteLine( "Message : {0}", e->Message );
}
catch ( ArgumentNullException^ e ) 
{
   Console::WriteLine( "ArgumentNullException caught!!!" );
   Console::WriteLine( "Source : {0}", e->Source );
   Console::WriteLine( "Message : {0}", e->Message );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( "Exception caught!!!" );
   Console::WriteLine( "Source : {0}", e->Source );
   Console::WriteLine( "Message : {0}", e->Message );
}
 try
 {
    IPHostEntry hostInfo = Dns.GetHostByName(hostName);
    // Get the IP address list that resolves to the host names contained in the
    // Alias property.
    IPAddress[] address = hostInfo.AddressList;
    // Get the alias names of the addresses in the IP address list.
    String[] alias = hostInfo.Aliases;

    Console.WriteLine("Host name : " + hostInfo.HostName);
    Console.WriteLine("\nAliases : ");
    for(int index=0; index < alias.Length; index++) {
      Console.WriteLine(alias[index]);
    }
    Console.WriteLine("\nIP address list : ");
    for(int index=0; index < address.Length; index++) {
       Console.WriteLine(address[index]);
    }
 }
 catch(SocketException e)
 {
    Console.WriteLine("SocketException caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
 }
 catch(ArgumentNullException e)
 {
Console.WriteLine("ArgumentNullException caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
 }
 catch(Exception e)
 {
     Console.WriteLine("Exception caught!!!");
     Console.WriteLine("Source : " + e.Source);
     Console.WriteLine("Message : " + e.Message);
 }
Public Sub DisplayHostName(hostName As [String])
    Try
        ' Call the GetHostByName method, passing a DNS style host name(for example,
        ' "www.contoso.com") as an argument to obtain an IPHostEntry instance, that 
        ' contains information for the specified host.
        
        Dim hostInfo As IPHostEntry = Dns.GetHostByName(hostName)
        ' Get the IP address list that resolves to the host names contained in 
        ' the Alias property.
        Dim address As IPAddress() = hostInfo.AddressList
        ' Get the alias names of the addresses in the IP address list.
        Dim [alias] As [String]() = hostInfo.Aliases
        
        Console.WriteLine(("Host name : " + hostInfo.HostName))
        Console.WriteLine(ControlChars.Cr + "Aliases : ")
        Dim index As Integer
        For index = 0 To [alias].Length - 1
            Console.WriteLine([alias](index))
        Next index
        Console.WriteLine(ControlChars.Cr + "IP address list : ")

        For index = 0 To address.Length - 1
            Console.WriteLine(address(index))
        Next index
    Catch e As SocketException
        Console.WriteLine("SocketException caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    Catch e As ArgumentNullException
        Console.WriteLine("ArgumentNullException caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    Catch e As Exception
        Console.WriteLine("Exception caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    End Try

備註

方法 GetHostByName 會查詢因特網 DNS 伺服器以取得主機資訊。 如果您傳遞空字串做為主機名,此方法會擷取本機計算機的標準主機名。

若要異步存取 DNS 資訊,請使用 BeginGetHostByNameEndGetHostByName 方法。

Ipv6Element.Enabled如果 屬性設定為 true,則Aliases傳回之實例的 IPHostEntry 屬性不會由這個方法填入,而且一律為空白。

注意

當您在應用程式中啟用網路追蹤時,此成員會發出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤

適用於