Dns.GetHostByName(String) Méthode
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Attention
GetHostByName is obsoleted for this type, please use GetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202
Attention
GetHostByName has been deprecated. Use GetHostEntry instead.
Attention
GetHostByName is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202
Obtient les données DNS pour le nom d'hôte DNS spécifié.
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
Nom DNS de l'hôte.
Objet IPHostEntry qui contient les données d'hôte pour l'adresse spécifiée dans hostName
.
- Attributs
hostName
a la valeur null
.
La longueur de hostName
est supérieure à 255 caractères.
Une erreur s'est produite lors de la résolution de hostName
.
L’exemple suivant utilise la GetHostByName méthode pour obtenir les informations DNS pour le nom d’hôte DNS spécifié.
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
La GetHostByName méthode interroge le serveur DNS Internet pour obtenir des informations sur l’hôte. Si vous passez une chaîne vide comme nom d’hôte, cette méthode récupère le nom d’hôte standard de l’ordinateur local.
Pour l’accès asynchrone aux informations DNS, utilisez les BeginGetHostByName méthodes et EndGetHostByName .
Si la propriété a la Ipv6Element.Enabledtrue
valeur , la Aliases propriété de l’instance IPHostEntry retournée n’est pas remplie par cette méthode et sera toujours vide.
Notes
Ce membre émet des informations de trace lorsque vous activez le suivi réseau dans votre application. Pour plus d’informations, consultez Suivi réseau dans .NET Framework.
Produit | Versions (Obsolète) |
---|---|
.NET | (Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9) |
.NET Framework | 1.1 (2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1) |
.NET Standard | (2.0, 2.1) |
Commentaires sur .NET
.NET est un projet open source. Sélectionnez un lien pour fournir des commentaires :