Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

http - Why URIBuilder encoding the encoded parameter twice in Java

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();
}
question from:https://stackoverflow.com/questions/65910472/why-uribuilder-encoding-the-encoded-parameter-twice-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...