Looking on the W3 Schools URL encoding webpage, it says that @
should be encoded as %40
, and that space
should be encoded as %20
.
I've tried both URLEncoder
and URI
, but neither does the above properly:
import java.net.URI;
import java.net.URLEncoder;
public class Test {
public static void main(String[] args) throws Exception {
// Prints me%40home.com (CORRECT)
System.out.println(URLEncoder.encode("me@home.com", "UTF-8"));
// Prints Email+Address (WRONG: Should be Email%20Address)
System.out.println(URLEncoder.encode("Email Address", "UTF-8"));
// http://www.home.com/test?Email%20Address=me@home.com
// (WRONG: it has not encoded the @ in the email address)
URI uri = new URI("http", "www.home.com", "/test", "Email Address=me@home.com", null);
System.out.println(uri.toString());
}
}
For some reason, URLEncoder
does the email address correctly but not spaces, and URI
does spaces currency but not email addresses.
How should I encode these 2 parameters to be consistent with what w3schools says is correct (or is w3schools wrong?)