简体   繁体   中英

URLDecode.decode method not working as expected in Java

I was trying to decode URL encoded post body and came across this problem.

I was using this method to decode (it decodes multiple encoded urls too) :

public static String decodeUrl(String url)
    {
        try {
            String prevURL="";
            String decodeURL=url;
            while(!prevURL.equals(decodeURL))
            {
                prevURL=decodeURL;
                decodeURL= URLDecoder.decode( decodeURL, "UTF-8" );
            }
            return decodeURL;
        } catch (UnsupportedEncodingException e) {
            return "Issue while decoding" +e.getMessage();
        }
    }

When the input url was "a%20%2B%20b%20%3D%3D%2013%25!" , the control somehow doens't show up after line decodeURL = when debugging . No exceptions are raised too.

The issue is that control doesn't go beyond the line"decodeURL" .

What might be causing the issue ? Please use debugger to hopefully mimic this problem.

Just tested it on Java 8u151. This throws an IllegalArgumentException on the second spin of the loop: "URLDecoder: Incomplete trailing escape (%) pattern". That's because after the first decoding you have "a + b == 13%!", and during the second decoding that % is supposed to introduce an encoding sequence but it does not. I think that's the expected behaviour, even if the standard library of other languages does not agree. Python 3.6 for example:

>>> from urllib.parse import unquote
>>> result = unquote('a%20%2B%20b%20%3D%3D%2013%25!')
>>> result
'a + b == 13%!'
>>> unquote(result)
'a + b == 13%!'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM