The behavior of Uri.IsWellFormedUriString is very confusing. Below code snippets:
List<string> absoluteUrls = new List<string>()
{
"https://api.contoso.com/query?q=世界&accessKey=acd%3D",
"https://api.contoso.com/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D",
"https://api.contoso.com/query?q=世界&accessKey=acd=",
"https://api.contoso.com/query?q=world&accessKey=acd%3D"
};
foreach (var url in absoluteUrls)
{
Console.WriteLine($"{url}: {Uri.IsWellFormedUriString(url, UriKind.Absolute)}");
}
List<string> relativeUrls = new List<string>()
{
"/query?q=世界&accessKey=acd%3D",
"/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D",
"/query?q=世界&accessKey=acd=",
"/query?q=world&accessKey=acd%3D"
};
foreach (var url in relativeUrls)
{
Console.WriteLine($"{url}: {Uri.IsWellFormedUriString(url, UriKind.Relative)}");
}
will output following results in both .Net core 3.1 and .Net framework 4.7.2:
| URL |
Result |
https://api.contoso.com/query?q=世界&accessKey=acd%3D |
False |
https://api.contoso.com/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D |
False |
https://api.contoso.com/query?q=世界&accessKey=acd= |
True |
https://api.contoso.com/query?q=world&accessKey=acd%3D |
True |
/query?q=世界&accessKey=acd%3D |
False |
/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D |
True |
/query?q=世界&accessKey=acd= |
False |
/query?q=world&accessKey=acd%3D |
True |
The "False" result of https://api.contoso.com/query?q=世界&accessKey=acd%3D gives me the impression that Uri.IsWellFormedUriString is not able to handle percent-encodings of reserved characters defined in RFC 3986, but I could not explain why https://api.contoso.com/query?q=world&accessKey=acd%3D return "True"
I also found that there are inconsistencies between the parsing result of UriKind.Absolute and UriKind.Relative for the same URI. /query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D return "True" while https://api.contoso.com/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D return "False".
The behavior of Uri.IsWellFormedUriString is very confusing. Below code snippets:
will output following results in both .Net core 3.1 and .Net framework 4.7.2:
https://api.contoso.com/query?q=世界&accessKey=acd%3Dhttps://api.contoso.com/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3Dhttps://api.contoso.com/query?q=世界&accessKey=acd=https://api.contoso.com/query?q=world&accessKey=acd%3D/query?q=世界&accessKey=acd%3D/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3D/query?q=世界&accessKey=acd=/query?q=world&accessKey=acd%3DThe "False" result of
https://api.contoso.com/query?q=世界&accessKey=acd%3Dgives me the impression that Uri.IsWellFormedUriString is not able to handle percent-encodings of reserved characters defined in RFC 3986, but I could not explain whyhttps://api.contoso.com/query?q=world&accessKey=acd%3Dreturn "True"I also found that there are inconsistencies between the parsing result of UriKind.Absolute and UriKind.Relative for the same URI.
/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3Dreturn "True" whilehttps://api.contoso.com/query?q=%E4%B8%96%E7%95%8C&accessKey=acd%3Dreturn "False".