How to find out if string has already been URL encoded?

后端 未结 11 1039
死守一世寂寞
死守一世寂寞 2020-11-30 03:46

How could I check if string has already been encoded?

For example, if I encode TEST==, I get TEST%3D%3D. If I again encode last string, I

11条回答
  •  感动是毒
    2020-11-30 04:19

    Check your URL for suspicious characters[1]. List of candidates:

    WHITE_SPACE ,", < , > , { , } , | , \ , ^ , ~ , [ , ] , . and `

    I use:

    private static boolean isAlreadyEncoded(String passedUrl) {
            boolean isEncoded = true;
            if (passedUrl.matches(".*[\\ \"\\<\\>\\{\\}|\\\\^~\\[\\]].*")) {
                    isEncoded = false;
            }
            return isEncoded;
    }
    

    For the actual encoding I proceed with:

    https://stackoverflow.com/a/49796882/1485527

    Note: Even if your URL doesn't contain unsafe characters you might want to apply, e.g. Punnycode encoding to the host name. So there is still much space for additional checks.


    [1] A list of candidates can be found in the section "unsafe" of the URL spec at Page 2. In my understanding '%' or '#' should be left out in the encoding check, since these characters can occur in encoded URLs as well.

提交回复
热议问题