0

In a link I have the { and } characters. In chrome/ff (and ie?), when I click that link it replaces { with %7B and } with %7D. Unfortunately, in opera it doesn't. If I URLEncoder.encode the link, then %7B becomes %257B in the other browsers (so then I get a 404 @ that resource). So when I click the link in opera I get java.lang.IllegalArgumentException: Illegal character in path at index. I'm confused about:

  1. Why doesn't opera encode { and }?
  2. Why throw an exception? Why not just encode the URL and continue?
  3. How to solve this without breaking the links (w/extra encoding) in other browsers?

Edit: we're using jersey, as it says in the stack trace:

java.lang.IllegalArgumentException: Illegal character in path at index
    java.net.URI.create(URI.java:859)
    javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:95)
    com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:879)
    com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:843)
    com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:804)
Josh
  • 801
  • 3
  • 11
  • 16

1 Answers1

0

Confusingly (i.e. it is badly named) URLEncoder is meant for passing data as parameters, not for encoding the URL itself.

We use org.apache.commons.httpclient.util.URIUtil.encodePath but it looks like this has gone now (since 1.4?) and the suggestion is to use java.net.URI instead: What happened to URIUtil.encodePath from commons-httpclient-3.1?

Have a look at this too: HTTP URL Address Encoding in Java

Community
  • 1
  • 1
Martin Wilson
  • 3,386
  • 1
  • 24
  • 29