Domanda

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?

È stato utile?

Soluzione

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
    }
  }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top