Encode and Decode URL in java
Special characters if needed to be sent through URL then it should be encoded and sent.
Following code in java is used to encode and decode URLs
import java.net.URLDecoder; import java.net.URLEncoder; import java.io.UnsupportedEncodingException; public class EncodeDecodeURL { public static void main(String[] args) throws UnsupportedEncodingException { String url = "éàî_@"; String encodedUrl = URLEncoder.encode(url, "UTF-8"); System.out.println("Encoded URL => " + encodedUrl); String decodedUrl = URLDecoder.decode(encodedUrl, "UTF-8"); System.out.println("Decoded URL => " + decodedUrl); } }
Remember always encode the URL string and form parameters to prevent all the vulnerability attacks.