Friday, 30 August 2013

Encode & Decode URL Parameters In Java

A small code example describing how to encode and decode URL query strings properly from Java. The code example is available from GitHub in the in the Java-Core-Examples directory.
String paramValue = "with spaces and special &!=; characters";

String encoded = URLEncoder.encode(paramValue, "UTF-8");
String decoded = URLDecoder.decode(encoded, "UTF-8");

System.out.println(paramValue);
System.out.println(encoded);
System.out.println(decoded);
The output is:
with spaces and special &!=; characters
with+spaces+and+special+%26%21%3D%3B+characters
with spaces and special &!=; characters

No comments:

Post a Comment