Comparing URLs with parameters in Java

These two URLs must be the same:

http://localhost?asdf=1&qwer=2
http://localhost?qwer=2&asdf=1

But using the 'equals ()' from the URL class, I understand that they are different. How can I compare them?

EDIT: Some background on this

I am creating a Selenium test for some URL mapping. I have an old url, the url to which it should be mapped, and the actual url. I need to compare the intended URL with the actual URL. A list of URLs is created by the client. Urls with the same parameters and values ​​are considered valid if the list of parameters is the same and they all have the correct values ​​(the order of these parameters does not matter)

+5
source share
5 answers

URL/URI . , , . . ,

Mr. - URL suck?!?

, ,

URL- java?

, , . , url.

,

+1

, , URL-, .

URL , . , URL - final, URL.equals(Object).

sameFile().
true, getQuery() - , String.split( "\&" ). , .
, , "#", .

+1

, . (, StringUtils )

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class URLDemo
{

    /**
     * @param args
     * @throws MalformedURLException
     */
    public static void main(String[] args)
        throws MalformedURLException
    {
        String url1 = "http://localhost?asdf=1&qwer=2";
        String url2 = "http://localhost?qwer=2&asdf=1";
        URL url11 = new URL(StringUtils.substringBefore(url1, "?"));
        URL url22 = new URL(StringUtils.substringBefore(url2, "?"));

        if (url11.equals(url22))
        {
            // url are equal but still need to check the parameters
            List<String> params1 = Arrays.asList(StringUtils.split(StringUtils.substringAfter(url1, "?"), "&"));
            List<String> params2 = Arrays.asList(StringUtils.split(StringUtils.substringAfter(url2, "?"), "&"));

            // need to check both ways
            if (params1.containsAll(params2) && params2.containsAll(params1))
            {
                System.out.println("URLs are the same");
            }
            else
            {
                System.out.println("URLs are different");
            }
        }
    }

}
0

, , -

URL url = new URL("http://localhost?asdf=1&qwer=2");
URL url2 = new URL("http://localhost?qwer=2&asdf=1");

System.out.println(isEqual(url, url2));

public static boolean isEqual(URL url1, URL url2) {

    boolean isEqual = url1.getAuthority().equals(url2.getAuthority()) &&
                    url1.getPort() == url2.getPort() &&
                    url1.getHost().equals(url2.getHost()) &&
                    url1.getProtocol().equals(url2.getProtocol());

    if (isEqual) {

        String query1 = url1.getQuery();
        String query2 = url2.getQuery();

        if (query1 != null && query2 != null) {

            if (query1.length() == query2.length()) {

                List<String> list1 = getParameters(query1);
                List<String> list2 = getParameters(query2);

                for (int index = 0; index < list1.size(); index++) {

                    String value1 = list1.get(index);
                    String value2 = list2.get(index);
                    if (!value1.equals(value2)) {

                        isEqual = false;
                        break;

                    }

                }

            } else {

                isEqual = false;

            }

        } else {

            isEqual = false;

        }

    }

    return isEqual;

}

protected static List<String> getParameters(String value) {

    List<String> parameters = new ArrayList<String>(25);

    String[] values = value.split("&");
    for (String par : values) {

        if (!par.contains("=")) {
            par += "=null";
        }

        parameters.add(par);

    }

    Collections.sort(parameters);

    return parameters;

}
0

, URL-

http://localhost?asdf=1&qwer=2
http://localhost?qwer=2&asdf=1

////////EDITED PART//////////

....

- openStream() URL.

- Save both of them as 2 StringObjects or StringBuilder.

- Now compare both of them with equals().

- This will solve the problem, I think so ....

-1
source

All Articles