java url hostname_Java – 在url中替换主机?

本文介绍如何使用Java的URI类替换URL中的主机名及其端口,并保持其他非权限部分不变。此外,还提供了处理显式但不必要的端口号的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

你使用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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值