I created the method to check if given URL parameter have valid URL inside it so encode it and replace with decoded value. but when i am trying to do that the uriBuilder.build.toString() encoding the encoded parameter twice.
e.g when i am passing this URL inside the method
http://example.com/query?q=random&redirect=https://localhost:8080/home
it returns: http://example.com/query?q=random&redirect=https%253A%252F%252Flocalhost%253A8080%252Fhome
import org.apache.http.client.utils.URIBuilder; import org.apache.commons.validator.routines.UrlValidator; import org.apache.http.NameValuePair;
public String getEncodedRedirectURL(String url) throws UnsupportedEncodingException, URISyntaxException { URIBuilder uriBuilder = new URIBuilder(url); UrlValidator urlValidator = new UrlValidator(); for (NameValuePair queryParam : uriBuilder.getQueryParams()) { if (urlValidator.isValid(queryParam.getValue())) { uriBuilder.setParameter(queryParam.getName(), URLEncoder.encode(queryParam.getValue(), StandardCharsets.UTF_8.name())); } } return uriBuilder.build().toString(); }
2.1m questions
2.1m answers
60 comments
57.0k users