1

I have an uri, whose parameter is like this,

id=ahshshs+24 When I am parsing the parameter by,

String id = uri.getQueryParameter("id");

I am getting the response as

ahshshs 24

The '+' is replaced by a space. I know this is because the uri gets encoded and it gets replaced. Is there a way, so as to get the value, without encoding?

1
  • While forming the URI, instead of using +, encode it, so that you could decode it later and interpret it as a + instead of a space. You can use %2B to encode it. Commented Jul 27, 2017 at 11:56

4 Answers 4

2

Use %2B instead + symbol

so parameter should be like Dahshshs%2B24

More details Characters and symbols

Sign up to request clarification or add additional context in comments.

Comments

0

While creating the URL encode it using %2B.

Refer link which states :

Space characters are replaced by `+'

A related question here, which has a lot more details on a similar topic (URL encoding)

Comments

0

I think that you had to encode your URL using java.net.URLEncoder.

String encodedParameter = URLEncoder.encode(paramToEncode, codification);

For example:

String encodedParameter = URLEncoder.encode("+", "UTF-8");

In this case, + will be encoded as %2B

Comments

0

Thanks for the responses, I had done it using UrlQuerySanitizer. Writing the code here. First, I extended the UrlQuerySanitizer class as

public class CustomUrlQuerySanitizer extends UrlQuerySanitizer {
    @Override
    protected void parseEntry(String parameter, String value) {
//        String unescapedParameter = unescape(parameter);
        ValueSanitizer valueSanitizer =
                getEffectiveValueSanitizer(parameter);

        if (valueSanitizer == null) {
            return;
        }
//        String unescapedValue = unescape(value);
        String sanitizedValue = valueSanitizer.sanitize(value);
        addSanitizedEntry(parameter, sanitizedValue);
    }
}

Then, parsed the url as,

String id = null;
if (uri != null) {
    UrlQuerySanitizer urlQuerySanitizer = new CustomUrlQuerySanitizer();

    urlQuerySanitizer.registerParameter("id",
            new UrlQuerySanitizer.IllegalCharacterValueSanitizer(
                    UrlQuerySanitizer.IllegalCharacterValueSanitizer.ALL_OK
            ));
    urlQuerySanitizer.parseUrl(uri.toString());
    id = urlQuerySanitizer.getValue("id");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.