suraj
suraj

Reputation: 1898

Who does decoding for URLencoding?

I have encoded my string(Say String a="123+gtyt") using URLEncoder class.The encoded string is String b. Then I am sending "String b" as a parameter appended to a URL. Lets say to http://example.com?request=b.

When I Decode the String at example.com using URLDecoder,The symbol + in my String is missing and I am not getting "String a" after decoding

Now When I print without decoding the "String b" at example.com.I get String a exactly.



So my doubt is whether the decoding is done by browser itself while redirecting?

Upvotes: 2

Views: 1581

Answers (1)

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

When you encode "123+gtyt" - it encodes the plus sign.

When you handle an HTTP request, servlet API automaticaly decodes it to "123+gtyt". If you decode it once again - it changes the "+" to a space.

So the key is - do not decode parameters explicitly.

For example:

    final String encoded = URLEncoder.encode("123+gtyt");
    final String decoded = URLDecoder.decode(encoded);
    System.out.println("decoded = " + decoded); // 123+gtyt
    System.out.println("URLDecoder.decode(decoded) = " 
              + URLDecoder.decode(decoded)); // prints 123 gtyt

Upvotes: 2

Related Questions