【发布时间】:2011-08-31 17:54:35
【问题描述】:
我想我可能在 Uri.IsWellFormedUriString 方法中发现了一个错误,这可能是因为它只符合RFC 2396 和RFC 2732 标准,而不是较新的RFC 3986,这使得上述两个已过时。
我认为发生的情况是,任何非 us-ascii 字符都会使其失败,因此其中包含 æ、ø、ö 或 å 等字符的 url 将使其返回 false。由于现在允许使用这些字符(wikipedia 等使用它们)我认为 Uri.IsWellFormedUriString 应该接受它们。下面的正则表达式取自 RFC 3986。
你怎么看?是否应该更新 Uri 类?
无论如何,这里有一些显示错误的示例代码:
static void Main(string[] args)
{
var urls = new []
{
@"/aaa/bbb/cccd",
@"/aaa/bbb/cccæ",
@"/aaa/bbb/cccø",
@"/aaa/bbb/cccå"
};
var regex = new Regex(@"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?");
Debug.WriteLine("");
foreach (var url in urls)
{
if (Uri.IsWellFormedUriString(url, UriKind.Relative))
Debug.WriteLine(url + " is a wellformed Uri");
if (regex.IsMatch(url))
Debug.WriteLine(url + " passed the Regex");
Debug.WriteLine("");
}
}
输出:
/aaa/bbb/cccd is a wellformed Uri
/aaa/bbb/cccd passed the Regex
/aaa/bbb/cccæ passed the Regex
/aaa/bbb/cccø passed the Regex
/aaa/bbb/cccå passed the Regex
【问题讨论】:
-
将
Uri.EscapeUriString方法应用于Uri.IsWellFormedUriString之前的每个网址对我有帮助