查看W3 Schools URL 编码网页,它说@
应该编码为%40
,space
应该编码为%20
。
我都尝试过URLEncoder
and URI
,但以上都没有正确:
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());
}
}
出于某种原因,URLEncoder
电子邮件地址是否正确但不是空格,并且URI
是否空格货币但不是电子邮件地址。
我应该如何编码这两个参数以与 w3schools 所说的正确(或者 w3schools 错误?)