URL encoding/decoding in java is very useful while working with the URL which is transmitted through the network. In case your URL contains some special characters in it then it might get replaced with some other special character.
Few important notes,
- Always use the same scheme to encode and decode the URL.
- URLs can be encoded multiple times hence always keep this in mind while comparing or retrieving the original URL.
- Before encoding, recommendation is to decode the URL first and see if resulted URL differs from original URL which means URL is already encoded.
Sample Java Code
public static void main(String args) throws UnsupportedEncodingException, URISyntaxException
// added the escape characters
String url = "https:\\\\ourhints.com\\test-java\\page1?attribute1=23&attribute2=45";
// encoding the orginal url
String encodeUrl = java.net.URLEncoder.encode(url, "UTF-8");
// getting the orginal url by decoding