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
If it's a url, use URI
URI uri = new URI("http", "//hello world", null);
String urlString = uri.toASCIIString();
System.out.println(urlString);
Solved with this:
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/util/UriUtils.html
Method encodeUri
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With