Search code examples
javaurldecode

Java - URLDecoder.decode(String s) vs URLDecoder.decode(String s, String enc)


What is the difference between

URLDecoder.decode(String s)

and

URLDecoder.decode(String s, String enc)

I had a cookie value like

val=%22myvalue%22

I am retrieving it:

Cookie[] cookies = request.getCookies();

String val = cookies[0].getValue();

But the value of val is %22myvalue%22

So I tried URLDecoder:

String val1 = URLDecoder.decode(val);
String val2 = URLDecoder.decode(val, "utf8");

And values of both are same, that is myvalue

So what is the difference between both?


Solution

  • URLDecoder.decode(String s)

    Decodes a x-www-form-urlencoded string. The platform's default encoding is used to determine what characters are represented by any consecutive sequences of the form "%xy".

    Note: It is deprecated.

    URLDecoder.decode(String s, String enc)

    Decodes a application/x-www-form-urlencoded string using a specific encoding scheme. The supplied encoding is used to determine what characters are represented by any consecutive sequences of the form "%xy".

    Entire info you can find here.