public sealed class Dns
Object
Dns
System
Networking
Obtains domain information from the Domain Name System as defined by IETF RFC 1035 and RFC 1036.
The Dns class creates and sends queries to obtain information about a host server from the Internet Domain Name System (DNS). In order to access DNS, the machine executing the query is required to be connected to a network. If the query is executed on a machine that does not have access to a domain name server, a SocketException is thrown.Information from the DNS query is returned in an instance of the IPHostEntry class. If the specified host has more than one entry in the DNS database, the IPHostEntry instance contains multiple IP addresses and aliases.
[Note: See the IPHostEntry class page for an example that uses the Dns class.]
System.Net Namespace
Dns Methods
Dns.BeginGetHostByName Method
Dns.BeginResolve Method
Dns.EndGetHostByName Method
Dns.EndResolve Method
Dns.GetHostByAddress(System.String) Method
Dns.GetHostByAddress(System.Net.IPAddress) Method
Dns.GetHostByName Method
Dns.GetHostName Method
Dns.Resolve Method
public static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback requestCallback, object stateObject);
Begins the asynchronous execution of a DNS query to obtain address information about the specified host.
- hostName
- A String containing the DNS name of the host.
- requestCallback
- A AsyncCallback delegate, or
null
.- stateObject
- An application-defined object, or
null
.
A IAsyncResult instance that contains information about the asynchronous operation.
Exception Type Condition ArgumentNullException hostName is null
.SocketException An error was encountered executing the DNS query . SecurityException The caller does not have permission to access DNS information.
This method starts an asynchronous request for information about the specified host. To retrieve the results of the query and release resources allocated by this method, call the System.Net.Dns.EndGetHostByName(System.IAsyncResult) method and specify the IAsyncResult object returned by this method. [Note: The System.Net.Dns.EndGetHostByName(System.IAsyncResult) method should be called exactly once for each call to System.Net.Dns.BeginGetHostByName(System.String,System.AsyncCallback,System.Object) .]
If the requestCallback parameter is not
null
, the method referenced by requestCallback is invoked when the asynchronous operation completes. The IAsyncResult object returned by this method is passed as the argument to the method referenced by requestCallback . The method referenced by requestCallback can retrieve the results of the query by calling System.Net.Dns.EndGetHostByName(System.IAsyncResult) .The stateObject parameter can be any object that the caller wishes to have available for the duration of the asynchronous operation. This object is available via the System.IAsyncResult.AsyncState property of the object returned by this method.
System.Net.Dns Class, System.Net Namespace
public static IAsyncResult BeginResolve(string hostName, AsyncCallback requestCallback, object stateObject);
Begins the asynchronous execution of a DNS query to resolve a host name or IP address.
- hostName
- A String containing the DNS name or IP address of the host.
- requestCallback
- A AsyncCallback delegate or
null
.- stateObject
- An application-defined object, or
null
.
A IAsyncResult instance that contains information about the asynchronous operation.
Exception Type Condition ArgumentNullException hostName is null
.SocketException An error was encountered executing the DNS query . SecurityException The caller does not have permission to access DNS information.
This method starts an asynchronous request for DNS information about the specified host. To retrieve the results of the query and release resources allocated by this method, call the System.Net.Dns.EndResolve(System.IAsyncResult) method, and specify the IAsyncResult object returned by this method. [Note: The System.Net.Dns.EndResolve(System.IAsyncResult) method should be called exactly once for each call to System.Net.Dns.BeginResolve(System.String,System.AsyncCallback,System.Object)].
If the requestCallback parameter is not
null
, the method referenced by requestCallback is invoked when the asynchronous operation completes. The IAsyncResult object returned by this method is passed as the argument to the method referenced by requestCallback. The method referenced by requestCallback can retrieve the results of the query by calling System.Net.Dns.EndResolve(System.IAsyncResult).The stateObject parameter can be any object that the caller wishes to have available for the duration of the asynchronous operation. This object is available via the System.IAsyncResult.AsyncState property of the object returned by this method.
System.Net.Dns Class, System.Net Namespace
public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult);
Ends an asynchronous query to obtain address information about the specified host.
- asyncResult
- The IAsyncResult object that holds the state information for the asynchronous operation.
A IPHostEntry object containing DNS address information about a host.
Exception Type Condition ArgumentNullException asyncResult is null
.
This method blocks if the asynchronous operation has not completed.The System.Net.Dns.EndGetHostByName(System.IAsyncResult) method completes an asynchronous request for DNS information that was started with a call to System.Net.Dns.BeginGetHostByName(System.String,System.AsyncCallback,System.Object). The object specified for the asyncResult parameter is required to be the same object as was returned by the System.Net.Dns.BeginGetHostByName(System.String,System.AsyncCallback,System.Object) method call that began the request.
If the System.Net.Dns.EndGetHostByName(System.IAsyncResult) method is invoked via the AsyncCallback delegate specified to the System.Net.Dns.BeginGetHostByName(System.String,System.AsyncCallback,System.Object) method, the asyncResult parameter is the IAsyncResult argument passed to the delegate's method.
System.Net.Dns Class, System.Net Namespace
public static IPHostEntry EndResolve(IAsyncResult asyncResult);
Ends an asynchronous query to resolve a host name or IP address.
- asyncResult
- The
System.IAsyncResult
object that holds the state information for the asynchronous operation.
A IPHostEntry object containing address information about a host.
Exception Type Condition ArgumentNullException asyncResult is null
.
This method blocks if the asynchronous operation has not completed.The System.Net.Dns.EndResolve(System.IAsyncResult) method completes an asynchronous request for DNS information that was started with a call to System.Net.Dns.BeginResolve(System.String,System.AsyncCallback,System.Object) . The object specified for the asyncResult parameter is required to be the same object as was returned by the System.Net.Dns.BeginResolve(System.String,System.AsyncCallback,System.Object) method call that began the request.
If the System.Net.Dns.EndResolve(System.IAsyncResult) method is invoked via the AsyncCallback delegate specified to the System.Net.Dns.BeginResolve(System.String,System.AsyncCallback,System.Object) method, the asyncResult parameter is the IAsyncResult argument passed to the delegate's method.
System.Net.Dns Class, System.Net Namespace
public static IPHostEntry GetHostByAddress(string address);
Queries DNS for the DNS host name of the specified IP address.
- address
- A String containing an IP address.
A IPHostEntry instance containing the host information.
Exception Type Condition ArgumentNullException address is null
.FormatException address is not a valid IP address. SocketException An error was encountered executing the DNS query . SecurityException The caller does not have permission to access DNS information.
The IP address specified by the address parameter is required to be in dotted-quad notation (for example, "192.168.1.2").
System.Net.Dns Class, System.Net Namespace
public static IPHostEntry GetHostByAddress(IPAddress address);
Queries DNS for the DNS host name of the specified IP address.
- address
- A IPAddress instance.
A IPHostEntry instance containing the host information.
Exception Type Condition ArgumentNullException address is null
.SocketException An error was encountered executing the DNS query . SecurityException The caller does not have permission to access DNS information.
System.Net.Dns Class, System.Net Namespace
public static IPHostEntry GetHostByName(string hostName);
Queries DNS for address information about the specified host.
- hostName
- A String containing the DNS name of the host.
A IPHostEntry object containing host information for the address specified in hostName .
Exception Type Condition ArgumentNullException hostName is null
.SocketException An error was encountered executing the DNS query . SecurityException The caller does not have permission to access DNS information.
[Note: To retrieve host information asynchronously, use the System.Net.Dns.BeginGetHostByName(System.String,System.AsyncCallback,System.Object) and System.Net.Dns.EndGetHostByName(System.IAsyncResult) methods.See the IPHostEntry class page for an example that uses the System.Net.Dns.GetHostByName(System.String) method.
]
System.Net.Dns Class, System.Net Namespace
public static string GetHostName();
Gets the DNS host name of the local machine.
A String containing the DNS host name of the local machine.
Exception Type Condition SocketException An error was encountered resolving the local host name. SecurityException The caller does not have permission to access DNS information.
System.Net.Dns Class, System.Net Namespace
public static IPHostEntry Resolve(string hostName);
Resolves a DNS host name or IP address to a IPHostEntry instance.
- hostName
- A String containing a DNS-style host name or IP address.
A IPHostEntry instance containing address information about the host specified in hostName .
Exception Type Condition ArgumentNullException hostName is null
.SocketException An error was encountered executing the DNS query . SecurityException The caller does not have permission to access DNS information.
The System.Net.Dns.Resolve(System.String) method queries a DNS server for the IP address associated with a host name or an IP address in dotted-quad notation.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.
The following example demonstrates the use of the System.Net.Dns.Resolve(System.String) method.
using System; using System.Net; public class DnsTest { public static void Main() { IPHostEntry hostInfo1 = Dns.Resolve("www.contoso.com"); DisplayHostInfo(hostInfo1); Console.WriteLine(); } public static void DisplayHostInfo(IPHostEntry hostInfo) { string[] aliases = hostInfo.Aliases; IPAddress[] addresses = hostInfo.AddressList; Console.WriteLine("The host name is: {0}", hostInfo.HostName); for(int x = 0; x < aliases.Length; x++) Console.WriteLine("Alias {0} == {1}", aliases[x], addresses[x]); } }The output is
The host name is: contoso.com
Alias www.contoso.com == 207.46.230.186
System.Net.Dns Class, System.Net Namespace