Java Reference
In-Depth Information
tipurpose Internet Mail Extensions (MIME) type to application/x-www-form-
urlencoded .
Note ThedataisencodedforHTTPPOSTandGEToperations.UnlikePOST,GET
requires a query string (a ?-prefixed string containing the encoded content) to be ap-
pended to the server program's URL.
The java.net package provides URLEncoder and URLDecoder classes to as-
sist you with the tasks of encoding and decoding form content.
URLEncoder applies the following encoding rules:
• Alphanumeric characters “a” through “z”, “A” through “Z”, and “0” through
“9” remain the same.
• Special characters “.”, “-”, “*”, and “_” remain the same.
• The space character “ ” is converted into a plus sign “+” on Internet Explorer
and “%20” on Firefox.
• Allothercharactersareunsafeandarefirstconvertedintooneormorebytesus-
ingsomeencodingscheme.Eachbyteisthenrepresentedbythethree-character
string % xy , where xy is the two-digit hexadecimal representation of that byte.
TherecommendedencodingschemetouseisUTF-8.However,forcompatibil-
ityreasons,theplatform'sdefaultencodingisusedwhenanencodingisn'tspe-
cified.
For example, using UTF-8 as the encoding scheme, the string "the string
ü@foo-bar" is converted to "the+string+%c3%bc%40foo-bar" . In UTF-8,
characterüisencodedastwobytesC3(hex)andBC(hex),andcharacter@isencoded
as one byte 40 (hex).
URLEncoder declares the following class method for encoding a string:
String encode(String s, String enc)
Thismethodtranslatesthe String argumentpassedto s into application/x-
www-form-urlencoded format using encoding scheme enc . It uses the supplied
encoding scheme to obtain the bytes for unsafe characters, and throws
java.io.UnsupportedEncodingException when enc 's value isn't suppor-
ted.
URLDecoder applies the following decoding rules:
• Alphanumeric characters “a” through “z”, “A” through “Z”, and “0” through
“9” remain the same.
• Special characters “.”, “-”, “*”, and “_” remain the same.
Search WWH ::




Custom Search