Use the URL
class. For example:
URL url = new URL(urlString);
String encodedQueryString = URLEncoder.encode(url.getQuery());
String encodedUrl = urlString.replace(url.getQuery(), encodedQueryString);
The third line might be different - for example constructing a new URL
from all its parts.