Which java library can I use to compare two URLs for equality?

This question is asked here:

but I am not completely satisfied with the answers. I need a way to compare two URLs for equality, and ideally I won’t write it manually . This library should 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&paramName2=value2 http://stackoverflow.com?paramName2=value2&paramName1=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&paramName2=value2 http://stackoverflow.com?paramName1=value1&paramName2=value3 

And other complicated things like this. Where can I find such a library?

By the way, here is the 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&paramName2=value2", "http://stackoverflow.com?paramName2=value2&paramName1=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&paramName2=value2", "http://stackoverflow.com?paramName1=value1&paramName2=value3"); } private void assertUrlsNotEqual(String u1, String u2) throws URISyntaxException { //...? } private void assertUrlsEqual(String u1, String u2) throws URISyntaxException { //...? } } 
+4
source share
2 answers

java.net.URI will compare two URLs without network requests ( java.net.URL method ), and you can use the normalize method to create a URL with an absolute path-canonical path.

There are some problems in your examples:

 http://stackoverflow.com?paramName= http://stackoverflow.com?paramName http://stackoverflow.com?paramName1=value1&paramName2=value2 http://stackoverflow.com?paramName2=value2&paramName1=value1 

The server is allowed to assign a value to the order of parameters and the presence of an equal sign, so these pairs are not equivalent according to RFC 3986 .

 http://stackoverflow.com?param name 1=value 1 http://stackoverflow.com?param%20name%201=value%201 

Not all URL libraries will treat them as valid because the former is not a valid URL in accordance with RFC 3986, although most user agents agree on how to convert the former to the latter.

+7
source

Update since 2018

There is an OkHttp Library that can properly compare URLs.

Here are articles about it - https://medium.com/square-corner-blog/okhttps-new-url-class-515460eea661 and <a2>

But keep in mind that he thinks these are different URLs:

 http://stackoverflow.com https://stackoverflow.com 

and

 stackoverflow.com www.stackoverflow.com 

You can do it as follows:

 HttpUrl url = HttpUrl.parse("http://google.com"); return url.equals(url2); 
0
source

Source: https://habr.com/ru/post/1497493/


All Articles