URI-getHost返回null.为什么? [英] URI - getHost returns null. Why?
问题描述
为什么第一个返回 null
,而第二个为什么返回 mail.yahoo.com
?
Why is the 1st one returning null
, while the 2nd one is returning mail.yahoo.com
?
这不是很奇怪吗?如果没有,这种行为背后的逻辑是什么?
Isn't this weird? If not, what's the logic behind this behavior?
下划线是罪魁祸首吗?为什么?
Is the underscore the culprit? Why?
public static void main(String[] args) throws Exception {
java.net.URI uri = new java.net.URI("http://broken_arrow.huntingtonhelps.com");
String host = uri.getHost();
System.out.println("Host = [" + host + "].");
uri = new java.net.URI("http://mail.yahoo.com");
host = uri.getHost();
System.out.println("Host = [" + host + "].");
}
推荐答案
如@hsz的评论中所述,已知错误.
As mentioned in comments by @hsz it is known bug.
但是,让我们调试并查看 URI
类的源代码.问题出在方法内部:
But, let's debug and look inside sources of URI
class. The problem is inside the method:
private int parseHostname(int start,int n)
:
在行 if((p< n)&&!at(p,n,':'))处解析第一个URI失败fail(主机名中的非法字符",p);
这是因为在扫描块中未预见到 _
符号,它仅允许使用字母,数字和-
符号( L_ALPHANUM
, H_ALPHANUM
, L_DASH
和 H_DASH
).
this is because _
symbol isn't foreseed inside scan block, it allows only alphas, digits and -
symbol (L_ALPHANUM
, H_ALPHANUM
, L_DASH
and H_DASH
).
是的,在 Java 7
中,此问题尚未解决.
And yes, this is not fixed yet in Java 7
.
这篇关于URI-getHost返回null.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!