在 Java 中进行 URL 查询字符串操作的好库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/218608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:34:35  来源:igfitidea点击:

A good library to do URL Query String manipulation in Java

javaurl

提问by Guillaume

I need to do a few very simple URL manipulations in Java. Like get the value for a parameter in the query, or update it, ... I was expecting to find a simple utility class doing that in the commons-lang package, but no. I know it is a simple problem, but if there is something already written, why do it again ? Do you know of any ?

我需要在 Java 中做一些非常简单的 URL 操作。就像获取查询中参数的值,或更新它,......我期待在 commons-lang 包中找到一个简单的实用程序类,但没有。我知道这是一个简单的问题,但如果已经写了一些东西,为什么还要再写?你知道吗?

I would like to have at least the following capabilities :

我希望至少具有以下功能:

String myUrl = "http://www.example.com/test.html?toto=1&titi=2";

// get the value of a parameter
String parameterValue = UrlUtils.getParameterValue(myUrl, "toto");
Assert.equals(parameterValue, "1");

// update a parameter
String newUrl = UrlUtils.updateParameter(myUrl, "toto", 3);
parameterValue = UrlUtils.getParameterValue(myUrl, "toto");
Assert.equals(parameterValue, "3");

Ideally, it would take care of all encoding related issues, and work with java.net.Url as well as with Strings.

理想情况下,它将处理所有与编码相关的问题,并与 java.net.Url 以及字符串一起使用。

Thanks for your help !

谢谢你的帮助 !

采纳答案by Vinko Vrsalovic

I think what you want is called a query string parser instead of an url manipulator and here's one: http://ostermiller.org/utils/CGIParser.java.html

我认为你想要的是一个查询字符串解析器而不是一个 url 操纵器,这里是一个:http: //ostermiller.org/utils/CGIParser.java.html

回答by piepera

Apache's httpcomponents library has a URL decoder: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html

Apache 的 httpcomponents 库有一个 URL 解码器:http: //hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html

Httpcomponents is the successor to commons http client.

Httpcomponents 是 commons http 客户端的继承者。

回答by youknowHyman

Indeed has released an efficient Java library for query string and number parsing:

Indeed 发布了一个高效的 Java 库,用于查询字符串和数字解析:

http://engineering.indeed.com/blog/2014/02/efficient-query-string-parsing-util-urlparsing/

http://engineering.indeed.com/blog/2014/02/efficient-query-string-parsing-util-urlparsing/

https://github.com/indeedeng/util/tree/master/urlparsing

https://github.com/indeedeng/util/tree/master/urlparsing

(Disclaimer: I am an engineering director at Indeed.)

(免责声明:我是 Indeed 的工程总监。)