gpt4 book ai didi

java - 删除所有 url 参数

转载 作者:行者123 更新时间:2023-11-30 06:31:39 25 4
gpt4 key购买 nike

我有一段代码可以从网址中删除选择性参数

    public String stripAndApplyTags(final List<NameValuePair> tags, final String url) throws URISyntaxException {
final URIBuilder existingLinkUrl = new URIBuilder(url);
List<NameValuePair> existingTags = existingLinkUrl.getQueryParams();
List<NameValuePair> newTags = new ArrayList<>();

for (Iterator<NameValuePair> queryParameterItr = existingTags.iterator(); queryParameterItr.hasNext();) {
NameValuePair queryParameter = queryParameterItr.next();
if (!queryParameter.getName().equals("test")) {
newTags.add(queryParameter);
}
}
newTags.addAll(tags);
return existingLinkUrl.clearParameters().setParameters(newTags).build().toString();
}

但问题是如果我的网址是这种形式 -

https://somexyzz/test=s9u_simh_gw_i1?ie=UTF8

有没有办法使用 URIBuilder 删除这个额外的测试标签?

最佳答案

Is there a way for me to remove this additional test tag using URIBuilder?

是的,您可以创建 URL对象并获取所需信息:

public static void main(String[] args) throws ParseException, MalformedURLException {
String foo = "https://somexyzz/test=s9u_simh_gw_i1?ie=UTF8";
URL myUrl = new URL(foo);
System.out.println("protocol = " + myUrl.getProtocol());
System.out.println("authority = " + myUrl.getAuthority());
System.out.println("host = " + myUrl.getHost());
System.out.println("port = " + myUrl.getPort());
System.out.println("path = " + myUrl.getPath());
System.out.println("query = " + myUrl.getQuery());
System.out.println("filename = " + myUrl.getFile());
System.out.println("ref = " + myUrl.getRef());
}

关于java - 删除所有 url 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45973294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com