Java URL encoding: URLEncoder vs. URI

人盡茶涼 提交于 2019-11-28 16:54:07

Although I think the answer from @fge is the right one, as I was using a 3rd party webservice that relied on the encoding outlined in the W3Schools article, I followed the answer from Java equivalent to JavaScript's encodeURIComponent that produces identical output?

public static String encodeURIComponent(String s) {
    String result;

    try {
        result = URLEncoder.encode(s, "UTF-8")
                .replaceAll("\\+", "%20")
                .replaceAll("\\%21", "!")
                .replaceAll("\\%27", "'")
                .replaceAll("\\%28", "(")
                .replaceAll("\\%29", ")")
                .replaceAll("\\%7E", "~");
    } catch (UnsupportedEncodingException e) {
        result = s;
    }

    return result;
}

URI syntax is defined by RFC 3986 (permissible content for a query string are defined in section 3.4). Java's URI complies to this RFC, with a few caveats mentioned in its Javadoc.

You will notice that the pchar grammar rule is defined by:

pchar = unreserved / pct-encoded / sub-delims / ":" / "@"

Which means a @ is legal in a query string.

Trust URI. It will do the correct, "legal" stuff.

Finally, if you have a look at the Javadoc of URLEncoder, you see that it states:

This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format.

Which is not the same thing as a query string as defined by the URI specification.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!