将 URL 与 Java 中的参数进行比较 [英] Comparing URLs with parameters in Java

查看:32
本文介绍了将 URL 与 Java 中的参数进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个网址应该是一样的:

These two urls should be the same:

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

但是使用 URL 类中的equals()",我发现它们是不同的.我如何比较它们?

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

关于这个问题的一些背景

我正在为某些 url 映射创建 Selenium 测试.我有旧的 url,它应该映射到 url 和实际的 url.我需要将应为 url 与实际 url 进行比较.url 列表由客户端创建.如果参数列表相同,并且它们都具有正确的值(这些参数的顺序无关紧要),则具有相同参数和值的 URL 被认为是有效的.

I'm creating a Selenium test for some url mapping. I have old url, url it should get mapped to and the actual url. I need to compare the should-be url to the actual url. List of urls is created by client. Urls with the same parameters and values are considered valid if the list of parameters is the same, and they all have the right values (order of those parameters is irrelevant)

推荐答案

所以,如果我猜对了,您想比较两个 URL,而不考虑查询部分的顺序.

So if I get it right, you want to compare two URL's, regardless of the order of the query part.

URL 类中似乎没有用于此的方法,因此您必须编写自己的方法.此外,URLfinal,您不能覆盖 URL.equals(Object).

There does not seem to be a method for this in the URL class, so you'll have to write your own. Also, URL is final, you cannot override URL.equals(Object).

您的方法可以从调用 sameFile().
如果计算结果为 true,则调用 getQuery(),并将其拆分为组件 - 可能使用 String.split("\&").从那时起,您评估参数是否相同.
另外,不要忘记检查#fragment"是否相等,如果这对您的应用程序很重要.

Your method could start with calling sameFile().
If that evaluates to true, you then call getQuery(), and split it into it's components - probably with String.split("\&"). From there on, you evaluate if the parameters are the same.
Also, don't forget to check if the "#fragment" is equal, if that is important to your application.

这篇关于将 URL 与 Java 中的参数进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆