This question has been asked here:
but I'm completely unsatisfied with the answers. I need a way to compare two URLs for equality and ideally I won't be writing it by hand. This library needs to understand that these urls are equal
http://stackoverflow.com https://stackoverflow.com/ https://stackoverflow.com/questions/ask https://stackoverflow.com/questions/ask/ http://stackoverflow.com?paramName= http://stackoverflow.com?paramName http://stackoverflow.com?paramName1=value1¶mName2=value2 http://stackoverflow.com?paramName2=value2¶mName1=value1 http://stackoverflow.com?param name 1=value 1 http://stackoverflow.com?param%20name%201=value%201 These URLs are not equal:
https://stackoverflow.com/questions/ask https://stackoverflow.com/questionz/ask http://stackoverflow.com?paramName1=value1¶mName2=value2 http://stackoverflow.com?paramName1=value1¶mName2=value3 And other complicated things like this. Where can I find such a library?
BTW, here is a unit test of this:
import org.junit.Test; import java.net.URI; import java.net.URISyntaxException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; public class UriTest { @Test public void equality() throws URISyntaxException { assertUrlsEqual("http://stackoverflow.com", "https://stackoverflow.com/"); assertUrlsEqual("https://stackoverflow.com/questions/ask", "https://stackoverflow.com/questions/ask/"); assertUrlsEqual("http://stackoverflow.com?paramName=", "http://stackoverflow.com?paramName"); assertUrlsEqual("http://stackoverflow.com?paramName1=value1¶mName2=value2", "http://stackoverflow.com?paramName2=value2¶mName1=value1"); assertUrlsEqual("http://stackoverflow.com?param name 1=value 1", "http://stackoverflow.com?param%20name%201=value%201"); } @Test public void notEqual() throws URISyntaxException { assertUrlsNotEqual("https://stackoverflow.com/questions/ask", "https://stackoverflow.com/questionz/ask"); assertUrlsNotEqual("http://stackoverflow.com?paramName1=value1¶mName2=value2", "http://stackoverflow.com?paramName1=value1¶mName2=value3"); } private void assertUrlsNotEqual(String u1, String u2) throws URISyntaxException { //...? } private void assertUrlsEqual(String u1, String u2) throws URISyntaxException { //...? } }
getHost()on yourURLand see if it equals the otherURL?stackoverflow.comandstackoverflow.com/really aren't equal by specification. They only happen to be equivalent for your purpose. This is why your requirement is not the stuff of public libs.