Hi, I want to use the URLEncoder/URLDecoder class (java.net.URLEncoder/URLDecoder) in an application and the methods : encode(String s, String enc)/decode(String s, String enc), but I don't know what can be the value of the String argument enc? I want to encode/decode in the "x-www-form-urlencoded" MIME content type. Thank you for your help.
The encoding parameter is the character encoding you're using. For example "UTF-8".
First you need to set the content-type as a 'x-www-form-urlencoded'. Then whatever content you would like to encode, encode it using "UTF-8".
For example:
For setting content to 'x-www-form-urlencoded':
URL url = new URL("http://www.xyz.com/SomeContext/SomeAction");
URLConnection urlConnection = url.openConnection();
....
....
urlConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
Or if you are using some JSP then you can write the following on top of it.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
< META http-equiv="Content-Type" content="text/html; charset=UTF-8">
< FORM action="someaction.jsp" enctype="application/x-www-form-urlencoded" name="InputForm" method="POST">
And to use URLEncoder:
String encodedString = URLEncoder.encode("hello","UTF-8");