某些URL constructors使用host
参数:
URL(字符串协议,字符串 host ,int端口,字符串文件)
URL(字符串协议,字符串 host ,整数端口,字符串文件, URLStreamHandler处理程序)
URL(字符串协议,字符串 host ,字符串文件)
1)为什么缺少userInfo元素(参数)?在URI构造函数中,我同时拥有userInfo
和host
参数。
2)如果我将 URL 构造函数的host
参数指定为“ user:psw@mysite.com” ,该怎么办?
URL u = new URL("http", "user:psw@mysite.com:20", "dir1");
System.out.println(u.getHost());
System.out.println(u.getAuthority());
System.out.println(u.getUserInfo());
System.out.println(u.getPort());
输出:
[user:psw@mysite.com:20] // getHost()
[user:psw@mysite.com:20] // getAuthority()
null // getUserInfo()
-1 // getPort()
那里发生了什么, new URL("http", "user:psw@mysite.com:20", "dir1")
是一个重大的编程错误,还是绝对可以吗?
边缘案例的唯一负面结果是getUserInfo()和getPort()解析不正确吗?
我对此表示怀疑:
URL u = new URL("http", "mysite.com", "dir1"); // no userInfo / port
System.out.println(u.getHost());
System.out.println(u.getAuthority());
输出现在没有括号(!):
mysite.com
mysite.com