This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details


Problem description

If it is I have a link

http://example.com/query?q=
Copy the code

I have a user input query like:

Random Word £500 Bank $Copy the code

I want the result to be a properly encoded URL:

http://example.com/query?q=random%20word%20%A3500%20bank%20%24
Copy the code

What is the best way to achieve this? I tried URLEncoder and created URI/URL objects, but none of them were completely correct.


Part of the high praise answered

Highest likes (1227 likes)

URLEncoder is the implementation method. Just remember that you only need to encode a single query string parameter name or value, not the entire URL, to ensure that it is not the query string parameter delimiter character &, nor the parameter name — value delimiter =.

String q = "random word £500 bank $";
String url = "https://example.com?q=" + URLEncoder.encode(q, StandardCharsets.UTF_8);
Copy the code

When you are still not in Java 10 or later, then use standardCharsets.utf_8.toString () as the character set argument, or when you are still not in Java 7 or later, then use ‘UTF-8’.


Note that valid Spaces in query parameters are represented by + instead of % 20. % 20 is typically used to represent Spaces in the URI itself (the part of the URI query string before the delimiter character?). Instead of in the query string (? After the parts).

Also note that there are three methods of encode(). One has no Charset as the second argument, and the other throws a String as the second argument to check for exceptions. The one with the Charset parameter is not recommended. Do not use it and always specify the Charset parameter. The Java documentation even explicitly recommends using utf-8 encoding as specified by RFC3986 and W3C.

All other characters are not secure and are first converted to one or more bytes using some encoding scheme. Each byte is then represented by the 3-string “% xy”, where xy is a two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if no encoding is specified, the platform’s default encoding is used.

Also check out this article every Web Developer must Know URL Encoding

Second most liked answer (189 likes)

I can’t use URLEncoder. Besides being misnamed (URLEncoder has nothing to do with urls), inefficient (it uses a StringBuffer instead of a Builder, and does a couple of other slow things), it’s also easy to mess up. On the contrary, I will use the URIBuilder or Spring org. Springframework. Web. Util. UriUtils. EncodeQuery or commonality Apache HttpClient, The reason is that you have to escape the query parameter name (BalusC’s answer Q) and parameter value in different ways. Sample code:

import org.apache.http.client.utils.URIBuilder; URIBuilder ub = new URIBuilder("http://example.com/query"); Ub. AddParameter ("q", "random word £500 bank \$"); String url = ub.toString(); // Result: http://example.com/query?q=random+word+%C2%A3500+bank+%24Copy the code

Translation from Stack Overflow. As for the translation article, leaf is also relatively new, it is inevitable that there are mistakes and omissions, welcome you to criticize in the comment section, thank you!