views:

50

answers:

2

I've got a GET method that looks like the following:

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

Such a path works just fine when typed directly into a browser, but the above line when run causes an IllegalArgumentException : Invalid URI.

I've looked at using the URIUtils class, but without success. Is there a way to automatically encode this (or to add a query string onto the URL without causing HttpClient to barf?).

+1  A: 

The mentioned URIUtils class has a method encodeAll(str) - so:

new GetMethod("http://host/path/?key=" 
       + URIUtil.encodeAll("[\"item\",\"item\"]"));
Bozho
That results in another exception: Exception in thread "main" java.lang.IllegalArgumentException: host parameter is null
Jason Nichols
@Jason Nichols - see my update. You have to encode only the parameter, not the whole uri
Bozho
You know, I swear I tried that early on and it didn't work =) Naturally it works now =) Thanks!
Jason Nichols
+1  A: 

You can also use java.net.URLEncoder:

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