How do I properly encode "[" and "]" in requests using Apache HttpClient?

I have a GET method that looks like this:

 GetMethod method = new GetMethod("http://host/path/?key=[\"item\",\"item\"]");

      

This path works just fine when entered directly into the browser, but the above line throws an IllegalArgumentException on startup: Invalid URI.

I have considered using the URIUtils class but with no success. Is there a way to auto-encode this (or add a query string to the url without calling the HttpClient for barf?).

+2


source to share


2 answers


The mentioned class URIUtils

has a method encodeAll(str)

- like this:



new GetMethod("http://host/path/?key=" 
       + URIUtil.encodeAll("[\"item\",\"item\"]"));

      

+2


source


You can also use java.net.URLEncoder:



String myURL = "http://host/path/?" +
  URLEncoder.encode("key=[\"item\",\"item\"]", UTF-8);

      

+2


source







All Articles