title: In Java, how to get canonicalized url
body: <p>Say i have space in a url, what is the right way to convert it to %20? no 'replace' suggestion please.</p>
  
  <p>For example, if you put "<a href="http://test.com/test" rel="nofollow">http://test.com/test</a> and test/a" into the browser window, it converts to <a href="http://test.com/test%20and%20test/a" rel="nofollow">http://test.com/test%20and%20test/a</a></p>
  
  <p>If I use URLEncoder, I get even the / converted. which is not what i want.</p>
  
  <p>Thanks,</p>
  
  <p>this is the right way, seems like. to add to the question, what if there is also some non ascii code in the path that I want convert to valid url with utf8 encode? e.g.： test.com:8080/test and test/pierlag2_carré/a?query=世界 I'd want it to be converted to test.com:8080/test%20and%20test/pierlag2_carr%C3%A9/a?query=%E4%B8%96%E7%95%8C</p>