11
votes

I have a hash key in one of my query params which can have + char with other special chars. The issue is when this URL is getting decoded URLDecoder converts + char into space. Is there a way we can enforce URLDecoder not to convert '+' into space.

3
Better than forcing this non-standard behaviour on the receiving side would be to fix the sending side to encode + characters in parameters correctly as %2B.Henry
To be safe, you should encode + into %2B instead...kennytm

3 Answers

7
votes

According to HTML URL Encoding Reference:

URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

and + sign itself must be encoded with %2B. So if you want to pass your hash as a GET parameter in URL, you should replace plus signs with %2B in your hash. Do not replace every + in the entire URL because you might ruin other string parameters which suppose to contain spaces.

12
votes

Do this on your string before decoding:

String plusEncoded = yourString.replaceAll("\\+", "%2b")

The decoder will then show + where it should've been

0
votes

There is a bug reference similar to this issue, and it's closed as "not an issue". Here I quote what the Assignee had told:

The Java API documentation at https://docs.oracle.com/javase/8/docs/api/java/net/URL.html clearly states that "The URLEncoder and URLDecoder classes can also be used, but only for HTML form encoding, which is not the same as the encoding scheme defined in RFC2396." . This means that it is not meant for URL encoding and will cause issues with spaces and plus signs in the path. Using URL or URI classes to construct the url will give expected results.

URL url = new URL(input);
System.out.println(url.toString()); //outputs http://www.example.com/some+thing

Reference: https://bugs.openjdk.java.net/browse/JDK-8179507