一个很好的库,用于在Java中执行URL查询字符串操作

时间:2008-10-20 14:19:29

标签: java url

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

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

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");

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

感谢您的帮助!

3 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:4)

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

Httpcomponents是commons http client的继承者。

答案 2 :(得分:3)

确实已经发布了一个用于查询字符串和数字解析的高效Java库:

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

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

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