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

大憨熊 提交于 2019-11-29 03:41:58
Bill

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

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

Why do the work yourself?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!