查看W3 Schools URL encoding webpage,它表明@
应该编码为%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是错误的?)
https://stackoverflow.com/questions/14321873
复制相似问题