title: Java URL encoding: URLEncoder vs. URI
body: <p>Looking on the <a href="http://www.w3schools.com/tags/ref_urlencode.asp">W3 Schools URL encoding webpage</a>, it says that <code>@</code> should be encoded as <code>%40</code>, and that <code>space</code> should be encoded as <code>%20</code>.</p>
  
  <p>I've tried both <code>URLEncoder</code> and <code>URI</code>, but neither does the above properly:</p>
  
  <pre><code>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());
      }
  }
  </code></pre>
  
  <p>For some reason, <code>URLEncoder</code> does the email address correctly but not spaces, and <code>URI</code> does spaces currency but not email addresses.</p>
  
  <p>How should I encode these 2 parameters to be consistent with what w3schools says is correct (or is w3schools wrong?)</p>