Dns.GetHostByName(String) Método
Definição
Cuidado
GetHostByName is obsoleted for this type, please use GetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202
Cuidado
GetHostByName is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202
Cuidado
Use GetHostEntry instead
Obtém as informações de DNS do nome do host DNS especificado.Gets the DNS information for the specified DNS host name.
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);
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);
[System.Obsolete("Use GetHostEntry instead")]
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
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
[<System.Obsolete("Use GetHostEntry instead")>]
static member GetHostByName : string -> System.Net.IPHostEntry
Public Shared Function GetHostByName (hostName As String) As IPHostEntry
Parâmetros
- hostName
- String
O nome DNS do host.The DNS name of the host.
Retornos
Um objeto IPHostEntry que contém informações de host do endereço especificado em hostName.An IPHostEntry object that contains host information for the address specified in hostName.
- Atributos
Exceções
hostName é null.hostName is null.
O tamanho de hostName é maior que 255 caracteres.The length of hostName is greater than 255 characters.
Um erro foi encontrado durante a resolução de hostName.An error is encountered when resolving hostName.
Exemplos
O exemplo a seguir usa o GetHostByName método para obter as informações de DNS para o nome de host DNS especificado.The following example uses the GetHostByName method to get the DNS information for the specified DNS host name.
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
Comentários
O GetHostByName método consulta o servidor DNS da Internet quanto a informações do host.The GetHostByName method queries the Internet DNS server for host information. Se você passar uma cadeia de caracteres vazia como o nome do host, esse método recuperará o nome do host padrão para o computador local.If you pass an empty string as the host name, this method retrieves the standard host name for the local computer.
Para acesso assíncrono a informações de DNS, use BeginGetHostByName os EndGetHostByName métodos e.For asynchronous access to DNS information, use the BeginGetHostByName and EndGetHostByName methods.
Se a Ipv6Element.Enabled propriedade for definida como true , a Aliases propriedade da IPHostEntry instância retornada não será preenchida por esse método e sempre estará vazia.If the Ipv6Element.Enabled property is set to true, the Aliases property of the IPHostEntry instance returned is not populated by this method and will always be empty.
Observação
Esse membro emite informações de rastreamento quando você habilita o rastreamento de rede em seu aplicativo.This member emits trace information when you enable network tracing in your application. Para obter mais informações, consulte rastreamento de rede na .NET Framework.For more information, see Network Tracing in the .NET Framework.