Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the safest way to determine if 2 URLs are the same?

Tags:

c#

url

c#-3.0

If I have URL A say http://www.example.com/ and another, say http://www.example.com. What would be the safest way to determine if both is the same, without querying for the web page and do a diff?

EXAMPLES:

  1. http://www.example.com/ VS http://www.example.com (Mentioned above)
  2. http://www.example.com/aa/../ VS http://www.example.com

EDIT: Clarifications: Just want to know if the URLs are the same in the context of being equivalent according to the RFC 1738 standard.

like image 825
Hao Wooi Lim Avatar asked Jun 26 '10 17:06

Hao Wooi Lim


1 Answers

In .Net, you can use the System.Uri class.

let u1 = new Uri("http://www.google.com/");;

val u1 : Uri = http://www.google.com/

let u2 = new Uri("http://www.google.com");;

val u2 : Uri = http://www.google.com/

u1.Equals(u2);;

val it : bool = true

For more fine-grained comparison, you can use the Uri.Compare method. There are also static methods to deal with various forms of escaping and encoding of characters in the Uri string, which will no doubt prove useful when dealing with the subject thoroughly.

like image 164
codekaizen Avatar answered Sep 26 '22 10:09

codekaizen