展望在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
做空间的货币而不是电子邮件地址。
我应该如何编码这两个参数是什么W3Schools的说是正确一致的(或者是W3Schools的错?)