URL Encoding with httpclient

Viewed 41240

I have a list of URLs which I need to get the content of. The URL is with special characters and thus needs to be encoded. I use Commons HtpClient to get the content.

when I use:

GetMethod get = new GetMethod(url);

I get a " Invalid "illegal escape character" exception. when I use

 GetMethod get = new GetMethod();
 get.setURI(new URI(url.toString(), false, "UTF-8"));

I get 404 when trying to get the page, because a space is turned to %2520 instead of just %20.

I've seen many posts about this problem, and most of them advice to build the URI part by part. The problem is that it's a given list of URLs, not a one that I can handle manually.

Any other solution for this problem?

thanks.

4 Answers

If you need to manipulate with request URIs it is strongly advisable to use URIBuilder shipped with Apache HttpClient.

try it out

GetMethod get = new GetMethod(url.replace(" ","%20")).toASCIIString());
Related