检查URL相等性的正确方法 [英] Proper way to check for URL equality

查看:149
本文介绍了检查URL相等性的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

URL u1 = new URL("http://www.yahoo.com/");
URL u2 = new URL("http://www.yahoo.com");

if (u1.equals(u2)) {
    System.out.println("yes");
}
if (u1.toURI().equals(u2.toURI())) {
    System.out.println("uri equality");
}
if (u1.toExternalForm().equals(u2.toExternalForm())) {
    System.out.println("external form equality");
}
if (u1.toURI().normalize().equals(u2.toURI().normalize())) {
    System.out.println("uri normalized equality");
}

这些检查都没有成功。只有路径不同:u1的路径为/,而u2的路径为。这些URL是否指向同一资源,是否可以在不打开连接的情况下检查此类内容?我是否误解了有关网址的基本信息?

None of these checks are succeeding. Only the path differs: u1 has a path of "/" while u2 has a path of "". Are these URLs pointing to the same resource and is there a way for me to check such a thing without opening a connection? Am I misunderstanding something fundamental about URLs?

编辑我应该声明需要进行非hacky检查。说空路径== /?是否合理?我希望没有这种代码

EDIT I should state that a non hacky check is desired. Is it reasonable to say that empty path == / ? I was hoping to not have this kind of code

推荐答案

来自2007 JavaOne:

From the 2007 JavaOne :


第二个拼图,名为More Joys of Sets,用户可以创建包含多个URL对象的HashMap键。同样,大多数观众都无法猜出正确的答案。

The second puzzle, aptly titled "More Joys of Sets" has the user create HashMap keys that consist or several URL objects. Again, most of the audience was unable to guess the correct answer.

观众在这里学到的重要事情是 URL对象的equals()方法是,实际上,破碎了。在这种情况下,如果两个URL对象解析为相同的IP地址和端口,则它们是相等的,而不仅仅是它们具有相同的字符串。然而,Bloch和Pugh指出了一个更严重的致命弱点:平等行为取决于您是否连接到网络,虚拟地址可以解析到同一主机,或者如果您不在网络上,解决方案是阻塞操作。因此,就经验教训而言,他们建议:

The important thing the audience learned here is that the URL object's equals() method is, in effect, broken. In this case, two URL objects are equal if they resolve to the same IP address and port, not just if they have equal strings. However, Bloch and Pugh point out an even more severe Achilles' Heel: the equality behavior differs depending on if you're connected to the network, where virtual addresses can resolve to the same host, or if you're not on the net, where the resolve is a blocking operation. So, as far as lessons learned, they recommend:

不要使用网址;使用URI 。 URI不会尝试比较地址或端口。此外,不要将URL用作Set元素或Map键。

对于API设计者,equals()方法不应该依赖于环境。例如,在这种情况下,如果计算机连接到Internet而不是独立计算机,则不应更改相等。

Don't use URL; use URI instead. URI makes no attempt to compare addresses or ports. In addition, don't use URL as a Set element or a Map key.
For API designers, the equals() method should not depend on the environment. For example, in this case, equality should not change if a computer is connected to the Internet versus standalone.






从URI等于文档:


From the URI equals documentation :


要使两个分层URI相等,它们的路径必须是等于并且他们的查询必须都是未定义的或者是相等的。

For two hierarchical URIs to be considered equal, their paths must be equal and their queries must either both be undefined or else be equal.

在你的情况下,两条路径是不同的。一个是/另一个是。

In your case, the two path are different. one is "/" the other is "".

根据URIRFC§6.2.3:

According to the URI RFC §6.2.3:


实施可能使用特定于方案的规则,进一步处理
成本,以减少漏报的可能性。例如,
因为http方案使用权限组件,具有
默认端口80,并且定义了一个等于
/的空路径,以下四个URI是等价的:

Implementations may use scheme-specific rules, at further processing cost, to reduce the probability of false negatives. For example, because the "http" scheme makes use of an authority component, has a default port of "80", and defines an empty path to be equivalent to "/", the following four URIs are equivalent:

 http://example.com
 http://example.com/
 http://example.com:/
 http://example.com:80/


似乎此实现不使用特定于方案的规则。

It seems that this implementation doesn't use scheme-specific rules.

资源:

  • sun.com - Java Puzzlers Serves Up Brain Benders Galore
  • javadoc - URI.equals()
  • URI RFC

这篇关于检查URL相等性的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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