|
Java™ by example!
|
|
|
How do I encode and decode 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):
Main.java:
outputs:
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.
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|