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?

有帮助吗?

解决方案

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
    }
  }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top