次の方法で共有


Dns.GetHostByName メソッド

指定した DNS ホスト名の DNS 情報を取得します。

Public Shared Function GetHostByName( _
   ByVal hostName As String _) As IPHostEntry
[C#]
public static IPHostEntry GetHostByName(stringhostName);
[C++]
public: static IPHostEntry* GetHostByName(String* hostName);
[JScript]
public static function GetHostByName(
   hostName : String) : IPHostEntry;

パラメータ

  • hostName
    ホストの DNS 名を格納する文字列。

戻り値

hostName で指定したアドレスのホスト情報を格納する IPHostEntry オブジェクト。

例外

例外の種類 条件
ArgumentNullException hostName が null 参照 (Visual Basic では Nothing) です。
SocketException hostName を解決するときにエラーが発生しました。
SecurityException 呼び出し元に、DNS 情報にアクセスするためのアクセス許可がありません。

解説

GetHostByName メソッドは、インターネット DNS サーバーでホスト情報を照会します。

DNS 情報への非同期アクセスでは、 BeginGetHostByName メソッドと EndGetHostByName メソッドを使用します。

使用例

[Visual Basic, C#, C++] GetHostByName を使用して、指定した DNS ホスト名の DNS 情報を取得する例を次に示します。

 
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 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

[C#] 
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);
}

[C++] 
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(S"Host name : {0}", hostInfo->HostName);
   Console::WriteLine(S"\nAliases : ");
   for (int index=0; index < alias->Length; index++)
      Console::WriteLine(alias->Item[index]);

   Console::WriteLine(S"\nIP address list : ");
   for (int index=0; index < address->Length; index++)
      Console::WriteLine(address->Item[index]);

} catch (SocketException* e) {
   Console::WriteLine(S"SocketException caught!!!");
   Console::WriteLine(S"Source : {0}", e->Source);
   Console::WriteLine(S"Message : {0}", e->Message);
} catch (ArgumentNullException* e) {
   Console::WriteLine(S"ArgumentNullException caught!!!");
   Console::WriteLine(S"Source : {0}", e->Source);
   Console::WriteLine(S"Message : {0}", e->Message);
} catch (Exception* e) {
   Console::WriteLine(S"Exception caught!!!");
   Console::WriteLine(S"Source : {0}", e->Source);
   Console::WriteLine(S"Message : {0}", e->Message);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

.NET Framework セキュリティ:

参照

Dns クラス | Dns メンバ | System.Net 名前空間