Not exactly that but close to:
final URI uri = URI.create(inputString);
final String[] queryParams = uri.getQuery().split("&");
You'd then split again on "="
each of the elements of queryParams
.
NOTE: do NOT use URLDecoder
directly to decode the values of query fragments; it will turn +
into spaces, which is wrong according to the URI spec (pchar
contains sub-delim
contains +
!)
An approaching solution would be to:
URLDecoder.decode(param.replace("+", "%2b"), "UTF-8")
To encode, use Guava's UrlPathSegmentEscaper
.
Demonstration: this simple main:
public static void main(final String... args)
throws UnsupportedEncodingException, URISyntaxException
{
System.out.println(URLDecoder.decode("a+b", "UTF-8"));
System.out.println(new URI("http", "foo.bar", "/baz", "op=a+b", null));
System.out.println(new URI("http", "foo.bar", "/baz", "op=a b", null));
}
prints:
a b // WRONG!
http://foo.bar/baz?op=a+b
http://foo.bar/baz?op=a%20b