57

I want to send a URI as the value of a query/matrix parameter. Before I can append it to an existing URI, I need to encode it according to RFC 2396. For example, given the input:

http://google.com/resource?key=value1 & value2

I expect the output:

http%3a%2f%2fgoogle.com%2fresource%3fkey%3dvalue1%2520%26%2520value2

Neither java.net.URLEncoder nor java.net.URI will generate the right output. URLEncoder is meant for HTML form encoding which is not the same as RFC 2396. URI has no mechanism for encoding a single value at a time so it has no way of knowing that value1 and value2 are part of the same key.

10
  • 1
    I am not sure I understand what result do you expect. I would use URLEncoder. Jan 14, 2009 at 18:26
  • According to the Javadoc for URL: "The URLEncoder and URLDecoder classes can also be used, but only for HTML form encoding, which is not the same as the encoding scheme defined in RFC2396."
    – Gili
    Jan 14, 2009 at 18:30
  • @Peter: Agreed, but the latter is dead. There is at least one up-voted answer so it won't show up in the list of unanswered questions even though the answer is technically wrong. If you want to help please go vote it down to zero.
    – Gili
    Jan 14, 2009 at 18:41
  • I wonder what would be result for example you give in your question. Jan 14, 2009 at 18:41
  • Sorry, I removed my comment about this being a duplicate of stackoverflow.com/questions/304806/… Jan 14, 2009 at 18:43

7 Answers 7

31

Jersey's UriBuilder encodes URI components using application/x-www-form-urlencoded and RFC 3986 as needed. According to the Javadoc

Builder methods perform contextual encoding of characters not permitted in the corresponding URI component following the rules of the application/x-www-form-urlencoded media type for query parameters and RFC 3986 for all other components. Note that only characters not permitted in a particular component are subject to encoding so, e.g., a path supplied to one of the path methods may contain matrix parameters or multiple path segments since the separators are legal characters and will not be encoded. Percent encoded values are also recognized where allowed and will not be double encoded.

8
  • URL is unreachable. But download.oracle.com/javaee/6/api/javax/ws/rs/core/… is available already
    – sergtk
    Dec 24, 2010 at 15:59
  • 3
    How exactly did you produce the expected output mentioned above using the UriBuilder? I have no clue how to tell it to encode the part before the "?". Thanks!
    – tbk
    May 24, 2011 at 15:38
  • 7
    If you are not use JAX-RS and are using Spring you could use Spring's UriUtils
    – Adam Gent
    Jul 16, 2011 at 3:51
  • 1
    @tbh, The point I was trying to make is that if you use UriBuilder it'll encode what needs to be encoded for you. If you want to manually encode sections of text it turns out that stackoverflow.com/questions/304806/… will work. Alternatively, you can use UriBuilder.fromPath("host/").matrixParam("key", "google.com/resource?key=value1 & value2").build() and you'll get back "host/…".
    – Gili
    Nov 7, 2011 at 16:22
  • 1
    Beware of using UriBuilder for "free" URLs that do not use JAX-RS's parameter scheme with curly braces. Even if it might be an exotic value, try this: UriBuilder.fromPath("http://www.query.example/").queryParam("key", "{val}").build(); will fail. "http://www.query.example?key=" + URLEncoder.encode("{val}", "UTF-8") will work.
    – ujay68
    Sep 10, 2015 at 19:28
20

You could also use Spring's UriUtils

9

I don't have enough reputation to comment on answers, but I just wanted to note that downloading the JSR-311 api by itself will not work. You need to download the reference implementation (jersey).

Only downloading the api from the JSR page will give you a ClassNotFoundException when the api tries to look for an implementation at runtime.

1
  • specifically, you need jsr311-api, jersey-server, and jersey-core jars.
    – yincrash
    Dec 30, 2009 at 22:43
3

I wrote my own, it's short, super simple, and you can copy it if you like: http://www.dmurph.com/2011/01/java-uri-encoder/

6
  • 5
    I guess people are hesitant to like this solution because maybe they fear it will have bugs etc. It looks pretty comprehensive, and has had a couple of bugs addressed already, so I think I will try it out. Java and Objective C both don't have built in routines to do this kind of encoding, which is just ... baffling Oct 10, 2012 at 7:54
  • Hmm, this is for the URI itself and not for the parameters?
    – Somatik
    Dec 11, 2012 at 10:15
  • Spaces should be escaped :) It will only include numbers, letters, and the 'mark' characters as unescaped May 7, 2013 at 20:38
  • @HerrGrumps What? Obj-C has def had built-in URL encoding for a long time.
    – Jadar
    Jan 31, 2017 at 5:55
  • @Jadar oh? ok, maybe I was wrong on Obj-C and/or Java not having it - sorry :) For reference maybe you can post a reply with some class/method names in the event that it might help someone out who might not know where to look (like me ;) ) Jan 31, 2017 at 10:37
2

It seems that CharEscapers from Google GData-java-client has what you want. It has uriPathEscaper method, uriQueryStringEscaper, and generic uriEscaper. (All return Escaper object which does actual escaping). Apache License.

9
  • Unfortunately it uses some other classes and interfaces, but I think you'll be able to modify it to suit your needs. Jan 14, 2009 at 19:37
  • "There has to be an easier way than this". It amazes me that this common use-case (building URIs) isn't easier to do. java.net.URI should do a better job.
    – Gili
    Jan 14, 2009 at 19:43
  • I was surprised at how messy this area is (honestly, I didn't even know there is such thing as special URI Encoding ... I learned something). Jan 14, 2009 at 19:44
  • Peter, I just realized this might work: jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/core/… I am using JAX-RS anyway for my application. I'll try it and report back.
    – Gili
    Jan 14, 2009 at 20:05
  • Peter, please add the following and I will mark it as the accepted answer: " javax.ws.rs.core.UriBuilder will do what you want: jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/core/… "
    – Gili
    Jan 14, 2009 at 20:11
-2

I think that the URI class is the one that you are looking for.

6
  • 3
    It doesn't help because it expects me to pass in a full query string. It has no way of knowing which part of the string needs to be encoded and which part does not. I need a method that takes in a raw parameter value and passes out the URL encoded form.
    – Gili
    Jan 14, 2009 at 18:37
  • Yes. Stackoverflow marks questions as answered if they have been up-voted once. This answer has been up-voted by one person and I down-voted it. It still marks my questions as answered last time I checked.
    – Gili
    Jan 14, 2009 at 19:30
  • My bad. You're right. I assumed "upvoted answer" meant ... you know, an answer with a positive number to its left ...
    – A. Rex
    Jan 14, 2009 at 19:32
  • Same here, please vote for stackoverflow.uservoice.com/pages/general/suggestions/…
    – Gili
    Jan 14, 2009 at 19:35
  • 1
    I will update the question. The reason the full URI can't be encoded is if I pass in "key=value with spaces&still part of value" the constructor has no way of knowing where the value begins and ends.
    – Gili
    Jan 14, 2009 at 20:09
-2

Mmhh I know you've already discarded URLEncoder, but despite of what the docs say, I decided to give it a try.

You said:

For example, given an input:

http://google.com/resource?key=value

I expect the output:

http%3a%2f%2fgoogle.com%2fresource%3fkey%3dvalue

So:

C:\oreyes\samples\java\URL>type URLEncodeSample.java
import java.net.*;

public class URLEncodeSample {
    public static void main( String [] args ) throws Throwable {
        System.out.println( URLEncoder.encode( args[0], "UTF-8" ));
    }
}

C:\oreyes\samples\java\URL>javac URLEncodeSample.java

C:\oreyes\samples\java\URL>java URLEncodeSample "http://google.com/resource?key=value"
http%3A%2F%2Fgoogle.com%2Fresource%3Fkey%3Dvalue

As expected.

What would be the problem with this?

5
  • 3
    It is similar to RFC2396 but is not the same. For example, try encoding spaces. URLEncoder will encode it as '+', URIs expect %20 instead. There are other differences.
    – Gili
    Jan 14, 2009 at 19:49
  • Ok but you wouldn't be encodgin: "value with space" but "value+with+space" like this:
    – OscarRyz
    Jan 14, 2009 at 20:05
  • like:java URLEncodeSample "google.com/resource?key=value+with+spaces" http%3A%2F%2Fgoogle.com%2Fresource%3Fkey%3Dvalue%2Bwith%2Bspaces
    – OscarRyz
    Jan 14, 2009 at 20:09
  • 1
    There are a whole slew of rules that one must follow for RFC 2369. Instead of playing games with the input string I'd rather find a class that encodes things properly.
    – Gili
    Jan 14, 2009 at 20:20
  • What do you intend to do? That would be very helpful to know in order to give you the right answer.
    – OscarRyz
    Jan 14, 2009 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.