Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode URL query parameters

How can I encode URL query parameter values? I need to replace spaces with %20, accents, non-ASCII characters etc.

I tried to use URLEncoder but it also encodes / character and if I give a string encoded with URLEncoder to the URL constructor I get a MalformedURLException (no protocol).

like image 640
Arutha Avatar asked Jun 01 '11 09:06

Arutha


People also ask

Should I encode URL parameters?

Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL.

How do you encode a query parameter in Java?

A URL String or form parameters can be encoded using the URLEncoder class – static encode (String s, String enc) method. For example, when a user enters following special characters, and your web application doesn't handle encoding, it will caused cross site script attack.

How do I pass a URL in a query string?

To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.

How do you encode and decode URL parameters in Java?

Encode the URLprivate String encodeValue(String value) { return URLEncoder. encode(value, StandardCharsets. UTF_8. toString()); } @Test public void givenRequestParam_whenUTF8Scheme_thenEncode() throws Exception { Map<String, String> requestParams = new HashMap<>(); requestParams.


1 Answers

URLEncoder has a very misleading name. It is according to the Javadocs used encode form parameters using MIME type application/x-www-form-urlencoded.

With this said it can be used to encode e.g., query parameters. For instance if a parameter looks like &/?# its encoded equivalent can be used as:

String url = "http://host.com/?key=" + URLEncoder.encode("&/?#");

Unless you have those special needs the URL javadocs suggests using new URI(..).toURL which performs URI encoding according to RFC2396.

The recommended way to manage the encoding and decoding of URLs is to use URI

The following sample

new URI("http", "host.com", "/path/", "key=| ?/#ä", "fragment").toURL();

produces the result http://host.com/path/?key=%7C%20?/%23ä#fragment. Note how characters such as ?&/ are not encoded.

For further information, see the posts HTTP URL Address Encoding in Java or how to encode URL to avoid special characters in java.


EDIT

Since your input is a string URL, using one of the parameterized constructor of URI will not help you. Neither can you use new URI(strUrl) directly since it doesn't quote URL parameters.

So at this stage we must use a trick to get what you want:

public URL parseUrl(String s) throws Exception {
     URL u = new URL(s);
     return new URI(
            u.getProtocol(), 
            u.getAuthority(), 
            u.getPath(),
            u.getQuery(), 
            u.getRef()).
            toURL();
}

Before you can use this routine you have to sanitize your string to ensure it represents an absolute URL. I see two approaches to this:

  1. Guessing. Prepend http:// to the string unless it's already present.

  2. Construct the URI from a context using new URL(URL context, String spec)

like image 143
Johan Sjöberg Avatar answered Sep 18 '22 15:09

Johan Sjöberg