Dns.Resolve () vs Dns.GetHostEntry ()

1

The Resolve method is obsolete, and Microsoft directs you to use GetHostEntry . However when using GetHostEntry the exception occurs:

  

This host is not known

When I use Resolve there is no exception and the program works correctly.

The Ip's used are from the LAN to which my pc is connected. I did a test by placing a remote Ip, from Google, and then GetHostEntry generated no exception, ie, it does not work with my lan's Ip's.

How can I solve this problem using GetHostEntry ?

    
asked by anonymous 17.08.2016 / 17:09

1 answer

1

This happens because the GetHostEntry method tries to make a reverse lookup before returning IP, if this procedure fails, error is returned WSAHOST_NOT_FOUND - in> Host not found .

Alternatively, you can use the GetHostAddresses , that unlike the GetHostEntry method, no reverse lookup is done, the IP is immediately returned as a result.

Example:

public static void DoGetHostAddresses(string hostname)
{
    IPAddress[] ips;    
    ips = Dns.GetHostAddresses(hostname);

    Console.WriteLine("GetHostAddresses({0}) returns:", hostname);

    foreach (IPAddress ip in ips)
    {
        Console.WriteLine("    {0}", ip);
    }
}

Font

    
17.08.2016 / 18:47