【问题标题】:Ip address GetHostName Null CheckIP 地址 GetHostName 空检查
【发布时间】:2013-07-29 08:12:50
【问题描述】:

我想用 C# 从特定 ip 范围获取域名

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

但是我遇到了这个错误 请求的名称有效并且在数据库中找到,但它没有正确的关联数据正在解析

有些IP没有域名。但我不能空检查GetHostEntry。 我试过这样但没有任何改变。我遇到了同样的错误

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

如何进行空检查以绕过此错误?

【问题讨论】:

  • 好像需要捕获异常来验证null
  • @Jonesy 我错过了 try catch 功能。它解决了问题

标签: c# dns ip reverse-dns


【解决方案1】:

您可以在 C# 中使用 try-catch 语句:

  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
    }
  }

【讨论】:

    猜你喜欢
    • 2012-08-03
    • 2013-02-22
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多