Java URL encoding: URLEncoder vs. URI
在W3学校URL编码网页上,它说应该编码为,并且应该编码为。@
%40
space
%20
我已经尝试了两者和,但上述两者都不正确:URLEncoder
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
我应该如何对这2个参数进行编码,以与w3schools所说的正确(或者w3schools是错误的?)保持一致。