你使用java.net.URI是对的.主机和端口(以及用户/密码,如果存在)统称为URI的权限组件:
public static String replaceHostInUrl(String originalURL,
String newAuthority)
throws URISyntaxException {
URI uri = new URI(originalURL);
uri = new URI(uri.getScheme().toLowerCase(Locale.US), newAuthority,
uri.getPath(), uri.getQuery(), uri.getFragment());
return uri.toString();
}
(一个URI的方案是required to be lowercase,所以虽然上面的代码可以说不能完全保留所有原始URL的非权限部分,但大写方案从一开始就不是真正合法的.当然,它不会影响URL连接的功能.)
请注意,您的某些测试存在错误.例如:
assertEquals("https://super/me/out?it=5", replaceHostInUrl("https://www.test.com:4300/me/out?it=5","super:443"));
assertEquals("http://super/me/out?it=5", replaceHostInUrl("http://www.test.com:4300/me/out?it=5","super:80"));
虽然https:// super / me / out?it = 5在功能上与https:// super:443 / me / out?it = 5相同(因为https的默认端口是443),如果指定显式端口在URI中,然后URI具有在其权限中指定的端口,以及它应该保持的方式.
更新:
如果要删除明确但不必要的端口号,可以使用URL.getDefaultPort()来检查它:
public static String replaceHostInUrl(String originalURL,
String newAuthority)
throws URISyntaxException,
MalformedURLException {
URI uri = new URI(originalURL);
uri = new URI(uri.getScheme().toLowerCase(Locale.US), newAuthority,
uri.getPath(), uri.getQuery(), uri.getFragment());
int port = uri.getPort();
if (port > 0 && port == uri.toURL().getDefaultPort()) {
uri = new URI(uri.getScheme(), uri.getUserInfo(),
uri.getHost(), -1, uri.getPath(),
uri.getQuery(), uri.getFragment());
}
return uri.toString();
}