在W3 Schools
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错误)相一致?
本文探讨了使用Java进行URL编码时遇到的问题,包括@符号和空格未能按预期编码的情况。通过示例代码展示了URLEncoder及URI类的用法,并讨论了如何使编码符合W3Schools的标准。
943

被折叠的 条评论
为什么被折叠?



