Поделиться через


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.

Атрибуты

Исключения

hostName имеет значение null.

Длина 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 используйте методы BeginGetHostByName и EndGetHostByName .

Ipv6Element.Enabled Если свойство имеет значение true, Aliases свойство возвращаемого IPHostEntry экземпляра не заполняется этим методом и всегда будет пустым.

Примечание

Этот элемент выдает сведения трассировки при включении трассировки сети в приложении. Дополнительные сведения см. в статье Трассировка сети в .NET Framework.

Применяется к