有人能解释一下为什么我在c#中使用Uri.CheckHostName(String)方法时会出现这种行为:
Console.WriteLine(Uri.CheckHostName("10"));
输出:IPv4
Console.WriteLine(Uri.CheckHostName("F"));
输出:DNS
你可以看到它们不是有效的IPv4或DNS值…
让我们看一下Uri调用的代码。CheckHostName,其中包括以下注释:
// Determines whether a string is a valid domain name label. In keeping
// with RFC 1123, section 2.1, the requirement that the first character
// of a label be alphabetic is dropped. Therefore, Domain names are
// formed as:
//
// <label> -> <alphanum> [<alphanum> | <hyphen> | <underscore>] * 62
本行重点:
要求[DNS]标签的第一个字符是删除字母。
该方法还调用此代码来检查有效字符。
总之,在我看来,它是按照设计工作的。