How does one encode query parameters to go on a url in Java? I know, this seems like an obvious and already asked question.
There are two subtleties I'm not sure of:
- Should spaces be encoded on the url as "+" or as "%20"? In chrome if I type in "http://google.com/foo=?bar me" chrome changes it to be encoded with %20
- Is it necessary/correct to encode colons ":" as %3B? Chrome doesn't.
Notes:
java.net.URLEncoder.encode
doesn't seem to work, it seems to be for encoding data to be form submitted. For example, it encodes space as+
instead of%20
, and encodes colon which isn't necessary.java.net.URI
doesn't encode query parameters
application/x-www-form-urlencoded
key/value pairs. See here for more: illegalargumentexception.blogspot.com/2009/12/… – Siva