Encoding and decoding a URL

When working with the HTTP protocol, some type of encoding is necessary because certain characters that are communicated over the network have special meaning for webservers, like @ and ?. For example, if you create a FORM in HTML using the GET or POST method, the webserver needs to differentiate between the different elements available on the form, etc. URL encoding is the most popular encoding scheme used for these types of things. It’s MIME format is called “x-www-form-urlencoded”.

The scheme for encoding is very simple (from the API):

   - 'a' through 'z', 'A' through 'Z', and '0' through '9' remain unchanged;
   - ' ' is converted to '+'
   - all other chars are converted into a 3 characters &hx where hx is the the 
     hexadecimal representation of the the lower 8-bits of the character. 

Main.java:

import java.net.*;
  
public class Main
{
   public static void main(String []args) {
      String s = "Hello, world!?!#@$#%";
      System.out.println("unencoded:t" + s);
 
      String encoded = URLEncoder.encode(s);
      System.out.println("encoded:t" + encoded);
 
      String decoded = URLDecoder.decode(encoded);
      System.out.println("decoded:t" + decoded);
   }
}

outputs:

Unencoded:	Hello, world!?!#@$#%
encoded:	Hello%2C+world%21%3F%21%23%40%24%23%25
decoded:	Hello, world!?!#@$#%

Try changing the string “hello world” with a complex url!

Notice that the method URLDecoder.decode throws an Exception in JDK1.2.2 (but it actually never throws one). This was removed in 1.3 and may affect compiling older code, as you had to catch the exception before 1.3. See the bug at the link below.