Saturday, April 28, 2012

Encode and Decode URLs Using UTF-8


This tip shows you how to encode and decode your URLs using the UTF-8 character encoding: 

import java.net.*;

public class URL_Encoder_Decoder{
 public static void main(String[] args){

      String url = "@sir de caractere@,nr. 1290 'paragraf 3'";
      try{
         String encode_url=URLEncoder.encode(url,"UTF-8");
         String decode_url=URLDecoder.decode(encode_url,"UTF-8");
         System.out.println(url);
         System.out.println(encode_url);  
         System.out.println(decode_url);      
         }catch(java.io.UnsupportedEncodingException e)
              {System.out.println("Error:"+e.getMessage());}   
 }
}

Output:@sir de caractere@,nr. 1290 'paragraf 3'
%40sir+de+caractere%40%2Cnr.+1290+%27paragraf+3%27
@sir de caractere@,nr. 1290 'paragraf 3'

No comments:

Post a Comment