Best way to determine if a domain name would be a valid in a “hosts” file?

前端 未结 2 528
时光说笑
时光说笑 2020-12-16 15:33

The Windows Hosts file allows you to associate an IP to a host name that has far greater freedom than a normal Internet domain name. I\'d like to create a function that dete

相关标签:
2条回答
  • 2020-12-16 15:58

    These methods are not reliable as you get some response even if the domain name is fake like "fasdfasdfasd.com".

    The best way is to send a WebResponse and wait for the response from the domain. Here is the complete code and explanations of this process (long code snippet so not copy-pasting here).

    http://www.dotnetfunda.com/articles/show/1072/validating-domain-name-in-aspnet

    Thanks

    0 讨论(0)
  • 2020-12-16 16:10

    How about the System.Uri.CheckHostName() method?

    private static bool IsValidDomainName(string name)
    {
        return Uri.CheckHostName(name) != UriHostNameType.Unknown;
    }
    

    Why do the work yourself?

    0 讨论(0)
提交回复
热议问题