views:

1050

answers:

2

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 ?

I would like to have at least the following capabilities :

String myUrl = "http://www.example.com/tes.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.

Thanks for your help !

A: 

Take a look at Commons Http Client. I'm not sure if there are any simple URL-handling classes/methods though.

(It wouldn't make sense for this type of functionality to be in commons-lang anyway - since this isn't really related to the language of Java itself).

matt b
No, there is no query string parser in httpclient.
Vinko Vrsalovic
+3  A: 

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

Vinko Vrsalovic