2

Today I had to escape URL (http://rfc3986.openrfc.org/ - for instance, replace 'space' character with %20) and surprisingly I couldn't find any API in Java which would take unescaped URL as 1 parameter and return escaped URL.

I am aware of URI class which is capable of performing escaping operation, however it's required to use multiargument contructor (http://download.oracle.com/javase/1.4.2/docs/api/java/net/URI.html#URI(java.lang.String, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)), using single-argument contructor simply doesn't perform escaping but throws an exception.

Luckily, I was already emedding JRuby library so I was able to use URI.escape method http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/Escape.html however I'm really surprised there is no easy way to achieve same effect in Java.

Am I missing some API?

michal.slocinski
  • 505
  • 1
  • 9
  • 22
  • possible duplicate of [Java - Convert String to valid URI object](http://stackoverflow.com/questions/573184/java-convert-string-to-valid-uri-object) – Uri Agassi May 01 '14 at 12:28
  • you might want to visit [this post](http://stackoverflow.com/questions/573184/java-convert-string-to-valid-uri-object) to get the solution. It has already been explained in detail and it should get you what you want. – ŁukaszBachman Apr 20 '11 at 08:12
  • yes, found this post however it suggests using old version of Apache HTTP client, new one does not provide URIUtil.encodeQuery any longer :-/ – michal.slocinski Apr 20 '11 at 08:33
  • Nice tip, didn't know that. So it just depends whether or not usage of old version is acceptable. In some cases it is. – ŁukaszBachman Apr 20 '11 at 09:09

0 Answers0