17

is there a class to encode a generic String following the RFC 3986 specification?

That is: "hello world" => "hello%20world" Not (RFC 1738): "hello+world"

Thanks

Mark
  • 67,098
  • 47
  • 117
  • 162

5 Answers5

8

Solved with this:

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/util/UriUtils.html

Method encodeUri

Deanna
  • 23,876
  • 7
  • 71
  • 156
Mark
  • 67,098
  • 47
  • 117
  • 162
6

If it's a url, use URI

URI uri = new URI("http", "//hello world", null);
String urlString = uri.toASCIIString();
System.out.println(urlString);
MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
4

Source : Twitter RFC3986 compliant encoding functions.

This method takes string and converts it to RFC3986 specific encoded string.

/** The encoding used to represent characters as bytes. */
public static final String ENCODING = "UTF-8";

public static String percentEncode(String s) {
    if (s == null) {
        return "";
    }
    try {
        return URLEncoder.encode(s, ENCODING)
                // OAuth encodes some characters differently:
                .replace("+", "%20").replace("*", "%2A")
                .replace("%7E", "~");
        // This could be done faster with more hand-crafted code.
    } catch (UnsupportedEncodingException wow) {
        throw new RuntimeException(wow.getMessage(), wow);
    }
}
Amit Tumkur
  • 2,752
  • 26
  • 27
  • 2
    [scribejava](https://github.com/scribejava/scribejava/blob/master/scribejava-core/src/main/java/com/github/scribejava/core/utils/OAuthEncoder.java#L29-L34) uses that approach as well, though if you use the example given in [RFC 5849 (OAuth 1.0)](https://tools.ietf.org/html/rfc5849#section-3.4) this method fails to encode a parameter like `a3=2+q` properly as this will encode the string to `a3=2%2Bq` instead of `a3=2%20q` as the `URLEncoder` encodes the string before the replace can take place – Roman Vottner Jul 16 '19 at 12:57
0

In don't know if there is one. There is a class that provides encoding but it changes " " into "+". But you can use the replaceAll method in String class to convert the "+" into what you want.

str.repaceAll("+","%20")

Radu Stoenescu
  • 3,187
  • 9
  • 28
  • 45
  • 1
    It's not just about the "+", it's about following the RFC 3986 specification completely instead of the RFC 1738 that is suitable for query parameters (that requires the "+"). – Mark May 03 '11 at 04:26
0

In the case of Spring Web applications, I was able to use this:

http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/util/UriComponentsBuilder.html

UriComponentsBuilder.newInstance()
  .queryParam("KEY1", "Wally's crazy empôrium=")
  .queryParam("KEY2", "Horibble % sign in value")
  .build().encode("UTF-8") // or .encode() defaults to UTF-8

returns the String

?KEY1=Wally's%20crazy%20emp%C3%B4rium%3D&KEY2=Horibble%20%25%20sign%20in%20value

A cross check on one of my favorite sites shows the same result, "Percent encoding for URIs". Looks good to me. http://rishida.net/tools/conversion/

Jason Dunkelberger
  • 1,207
  • 14
  • 18