Reputation: 1230
I am writing a java program where my url need to be decoded and I am using
URLDecoder.decode(url,"UTF-8")
method to achieve it.But, unfortunately it is not decoding completely. For eg. above method decodes %3d
to =
, %2B
to +
but it is not decoding few chars like %3A%2F
which remain unchanged even after decoding. Please tell me if I am missing something.
Upvotes: 4
Views: 2956
Reputation: 1650
First, everything is working as expected. Your problem is, that the input-string is encoded twice. So simply decode it two times.
Example:
%253A
%3A
:
Code:
String input = "40.2%2522%26url%3Dhttp%253A%252F%252Fr1";
String output1 = URLDecoder.decode(input, "UTF-8");
String output2 = URLDecoder.decode(output1, "UTF-8");
System.out.println(input);
System.out.println(output1);
System.out.println(output2);
Output:
40.2%2522%26url%3Dhttp%253A%252F%252Fr1
40.2%22&url=http%3A%2F%2Fr1
40.2"&url=http://r1
Note: If it's uncertain how many times the string is encoded you could repeat decoding until result remains constant.
String input = "40.2%2522%26url%3Dhttp%253A%252F%252Fr1";
String output = input;
do {
input = output;
output = URLDecoder.decode(input, "UTF-8");
} while (!input.equals(output));
Upvotes: 8