C# 确定域名在“主机”文件中是否有效的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/967516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:26:23  来源:igfitidea点击:

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

c#validationdomain-namehosts-file

提问by Jeff Moser

The Windows Hosts fileallows you to associate an IP to a host namethat has far greater freedom than a normal Internet domain name. I'd like to create a function that determines if a given name would be a valid "host" file domain name.

Windows主机文件允许您将 IP 与主机名相关联,该主机名比普通 Internet 域名具有更大的自由度。我想创建一个函数来确定给定的名称是否是有效的“主机”文件域名。

Based on this answerand experimentation of what works and doesn't, I came up with this function:

基于这个答案和对有效和无效的实验,我想出了这个功能:

private static bool IsValidDomainName(string domain)
{
    if (String.IsNullOrEmpty(domain) || domain.Length > 255)
    {
        return false;
    }

    Uri uri;

    if (!Uri.TryCreate("http://" + domain, UriKind.Absolute, out uri))
    {
        return false;
    }

    if (!String.Equals(uri.Host, domain, StringComparison.OrdinalIgnoreCase) || !uri.IsWellFormedOriginalString())
    {
        return false;
    }

    foreach (string part in uri.Host.Split('.'))
    {
        if (part.Length > 63)
        {
            return false;
        }
    }

    return true;
}

It also has the benefit that it should work with Unicode names (where a basic regex would fail).

它还有一个好处是它应该使用 Unicode 名称(基本正则表达式会失败)。

Is there a better/more elegant way to do this?

有没有更好/更优雅的方法来做到这一点?

UPDATE: As suggested by Bill, the Uri.CheckHostNamemethod almost does what I want, but it doesn't allow for host names like "-test" that Windows allows in a "hosts" file. I would special case the "-" part, but I'm concerned there are more special cases.

更新:正如Bill所建议的,Uri.CheckHostName方法几乎可以满足我的要求,但它不允许使用 Windows 在“hosts”文件中允许的“-test”之类的主机名。我会将“-”部分放在特殊情况下,但我担心还有更多特殊情况。

采纳答案by Bill

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

怎么样System.Uri.CheckHostName()方法?

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

Why do the work yourself?

为什么要自己动手?

回答by Sheo Narayan

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

即使域名是假的,如“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).

最好的方法是发送一个 WebResponse 并等待来自域的响应。这是此过程的完整代码和说明(代码片段较长,因此请勿在此处复制粘贴)。

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

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

Thanks

谢谢