October 16, 2014

URL Encoding in Java

Whenever we are passing data to any url or web services, some characters of the data might be un recognised. Hence, it is always better to encode data before sending to URLs or web services, which we call as URL encoding. We have pre defined apis in java to do this encoding.

Below is an example to show how to encode a string in java.

String str="Hello World";
java.net.URLEncoder..encode(str, "UTF-8");

The output for this code would be:

Hello+World.