title: URL percent encoding in Java
body: <pre class="lang-java prettyprint-override"><code>URL url = new URL("http://www.example.com/data.php?q=%FD");
  logger.info("url: " + url);
  URI uri = url.toURI();
  logger.info("uri ASCII: " + uri.toASCIIString());
  logger.info("uri str  : " + uri.toString());
  logger.info("query    : " + uri.getQuery());
  logger.info("decoded  : " + URLDecoder.decode(ur.getRawQuery(), "WINDOWS-1252"));
  
  String scheme = uri.getScheme();
  String auth   = uri.getAuthority();
  String path   = uri.getPath();
  String query  = uri.getQuery();
  
  URI cleanedURI = new URI(scheme, auth, path, query, null);
  logger.info("cleaned uri ASCII: " + cleanedURI.toASCIIString());
  logger.info("cleaned uri str  : " + cleanedURI.toString());
  </code></pre>
  
  <p>The output is:</p>
  
  <pre class="lang-none prettyprint-override"><code>url: http://www.example.com/data.php?q=%FD
  
  uri ASCII: http://www.example.com/data.php?q=%FD
  uri str  : http://www.example.com/data.php?q=%FD
  
  query: q=�
  decoded: q=ý
  
  cleaned uri ASCII: http://www.example.com/data.php?q=%EF%BF%BD
  cleaned uri str  : http://www.example.com/data.php?q=�
  </code></pre>
  
  <p>So, when I split the URI into parts, and then construct again, I cannot get back the original URL. How can I get back the original URL, which is a correctly percent-encoded, valid URL.</p>
  
  <p><strong>Instead of getting %EF%BF%BD I need to get the original %3F.</strong></p>
  
  <p>(Actually what I am trying to achieve is to manipulate certain parts of the URL in a clean way, such as removing the fragment, but this has not much relation to my question.)</p>