Question

I want to get domain names from specific ip range with C#

 IPAddress addr = IPAddress.Parse("100.10.100."+i);
    entry = Dns.GetHostEntry(addr);

But I encountered this error The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for

Some Ip hasn't domain names.But I can't null check GetHostEntry. I tried like that but nothing changed.I encountered same error

 if(Dns.GetHostEntry(addr)!=null)

How can I make null check for bypass this error?

Was it helpful?

Solution

You can use the try-catch statement in C#:

  for (int i = 1; i <= 255; i++) {
    IPAddress addr = IPAddress.Parse("100.10.100."+i);
    try
    {
      entry = Dns.GetHostEntry(addr);
      //some other codes that cause exception
    }
    catch(SocketException ex)
    {
      //do something
    }
    catch(Exception ex)
    {
      //catch all other exceptions
    }
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top