Dns.Resolve(String) 方法
定义
注意
Resolve is obsoleted for this type, please use GetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202
注意
Resolve is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202
注意
Use GetHostEntry instead
将 DNS 主机名或 IP 地址解析为 IPHostEntry 实例。Resolves a DNS host name or IP address to an IPHostEntry instance.
public:
static System::Net::IPHostEntry ^ Resolve(System::String ^ hostName);
[System.Obsolete("Resolve is obsoleted for this type, please use GetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")]
public static System.Net.IPHostEntry Resolve (string hostName);
public static System.Net.IPHostEntry Resolve (string hostName);
[System.Obsolete("Resolve is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")]
public static System.Net.IPHostEntry Resolve (string hostName);
[System.Obsolete("Use GetHostEntry instead")]
public static System.Net.IPHostEntry Resolve (string hostName);
[<System.Obsolete("Resolve is obsoleted for this type, please use GetHostEntry instead. https://go.microsoft.com/fwlink/?linkid=14202")>]
static member Resolve : string -> System.Net.IPHostEntry
static member Resolve : string -> System.Net.IPHostEntry
[<System.Obsolete("Resolve is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")>]
static member Resolve : string -> System.Net.IPHostEntry
[<System.Obsolete("Use GetHostEntry instead")>]
static member Resolve : string -> System.Net.IPHostEntry
Public Shared Function Resolve (hostName As String) As IPHostEntry
参数
- hostName
- String
DNS 样式的主机名或 IP 地址。A DNS-style host name or IP address.
返回
一个 IPHostEntry 实例,包含有关 hostName 中指定的主机的地址信息。An IPHostEntry instance that contains address information about the host specified in hostName.
- 属性
例外
hostName 为 null。hostName is null.
hostName 的长度超过 255 个字符。The length of hostName is greater than 255 characters.
解析 hostName 时遇到错误。An error is encountered when resolving hostName.
示例
下面的示例使用 Resolve 方法将 IP 地址解析为 IPHostEntry 实例。The following example uses the Resolve method to resolve an IP address to an IPHostEntry instance.
try
{
IPHostEntry^ hostInfo = Dns::Resolve( hostString );
// 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 ( NullReferenceException^ e )
{
Console::WriteLine( "NullReferenceException 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.Resolve(hostString);
// 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(NullReferenceException e)
{
Console.WriteLine("NullReferenceException 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);
}
Try
' Call the Resolve method passing a DNS style host name or an IP address in
' dotted-quad notation (for example, "www.contoso.com" or "207.46.131.199") to
' obtain an IPHostEntry instance that contains address information for the
' specified host.
Dim hostInfo As IPHostEntry = Dns.Resolve(hostString)
' 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 NullReferenceException
Console.WriteLine("NullReferenceException 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
注解
Resolve方法在 DNS 服务器中查询与主机名或 ip 地址关联的 ip 地址。The Resolve method queries a DNS server for the IP address associated with a host name or IP address.
如果 hostName 是与多个 IP 地址关联的 DNS 样式的主机名,则仅返回解析为该主机名的第一个 ip 地址。When hostName is a DNS-style host name associated with multiple IP addresses, only the first IP address that resolves to that host name is returned.
如果将 Ipv6Element.Enabled 属性设置为 true ,则返回的实例的 Aliases 属性不会 IPHostEntry 通过此方法填充,并且始终为空。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.
备注
在应用程序中启用网络跟踪后,此成员将发出跟踪信息。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.