Wednesday, December 31, 2008

java url encode, get rid of white space from url

Sometime we need to encode URL to the application/x-www-form-urlencoded MIME format.

public static String encode(String s,
String enc)
throws UnsupportedEncodingException
Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.


For example of encoding with UTF-8, using following code:
String encodeTargetUrl = URLEncoder.encode(targetUrl, "UTF-8");

No comments:

Post a Comment