Java: url encoding leaves "allowed" character intact

A simple question from a Java newbie. I want to encode the URL so that non-standard characters are converted to their hexadecimal value (i.e.% XX), while the characters that everyone expects to see in the letter-letter, numbers, slashes, question marks, etc. Will be left untouched.

For example, coding

"hi/hello?who=moris\\boris"

should appear with

"hi/hello?who=moris%5cboris"

ideas?

+3
source share
6 answers

This is actually a rather complicated problem. And the reason this is complicated is because different parts of the URL need to be processed (encoded) differently.

, - URL- URL URI, .


, , , . , () "?" ( ) (escape it).

0

, URL-. URL

public static String escapeSpecialCharacters(String input) {
        StringBuilder resultStr = new StringBuilder();
        for (char ch : input.toCharArray()) {
            if (isSafe(ch)) {
                resultStr.append(ch);
            } else{
                resultStr.append('%');
                resultStr.append(toHex(ch / 16));
                resultStr.append(toHex(ch % 16));                   
            }
        }

        return resultStr.toString();
    }

    private static char toHex(int ch) {
        return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10);
    }

    private static boolean isSafe(char ch) {
    return ((ch>='A' && ch<='Z') || (ch>='a' && ch<='z') || (ch>='0' && ch<='9') || "-_.~".indexOf(ch)>=0); 
}
+1

URLEncoder.encode(url, "UTF-8"), . Javadoc.

0

org.apache.commons.codec.net.URLCodec (, \, ). , , URL-, . , ? = , , .

0

Source: https://habr.com/ru/post/1792579/


All Articles