URLEncoder not able to translate space character

This behaves as expected. The URLEncoder implements the HTML Specifications for how to encode URLs in HTML forms.

From the javadocs:

This class contains static methods for
converting a String to the
application/x-www-form-urlencoded MIME
format.

and from the HTML Specification:

application/x-www-form-urlencoded

Forms submitted with this content type
must be encoded as follows:

  1. Control names and values are escaped. Space characters are replaced
    by `+’

You will have to replace it, e.g.:

System.out.println(java.net.URLEncoder.encode("Hello World", "UTF-8").replace("+", "%20"));

Leave a Comment