比较Url区分大小写

时间:2013-06-11 14:30:04

标签: .net http

是否有任何类方法可以测试两个外壳不同的网址是否相同?

这些是相同的:

  1. www.mysite.com
  2. Www.MYsite.COm
  3. 这些不一样:

    1. www.youtube.com/v=AAAABBBB
    2. www.youtube.com/v=aaaaBBBB
    3. EDIT 我不认为Uri课程足够

      这两个是相同的链接

      1. stackoverflow.com/questions
      2. stackoverflow.com/QUESTIONS

2 个答案:

答案 0 :(得分:1)

请注意,www.youtube.com/v=ObgtZwwiKqg是不正确的网址。正确的URL包含查询符号,例如www.youtube.com/watch?v=ObgtZwwiKqg

如何忽略查询路径并仅比较查询参数?如果您的网址中包含查询?,那么您可以删除所有内容以进行查询。如果没有,您至少可以使用UriPartial.Authority删除域名。

例如:

Uri a = new Uri("http://www.google.com/subdirectory?v=aaBB");
Uri b = new Uri("http://www.Google.com/SUBdirectory?v=AAbb");

string aParams = a.ToString().Replace(a.GetLeftPart(UriPartial.Path), String.Empty);
string bParams = b.ToString().Replace(b.GetLeftPart(UriPartial.Path), String.Empty);
if (aParams.Equals(bParams)) // with case
{
    // they are equal
}

答案 1 :(得分:0)

需要使用Uri类,并检查AbsolutePath属性

string url1 = "http://www.youtube.com/v=AAAABBBB";
string url2 = "http://www.youtube.com/v=aaaaBBBB";

Uri u1 = new Uri(url1);
Uri u2 = new Uri(url2);

if(string.Compare(u1.Host, u2.Host, StringComparison.CurrentCultureIgnoreCase) == 0)
{
    if(u1.AbsolutePath == u2.AbsolutePath)
        Console.WriteLine("Equals");
    else
        Console.WriteLine("Not equal path");
}
else
    Console.WriteLine("Not equal host");