0

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?

Debanjan
  • 2,817
  • 2
  • 24
  • 43
  • 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. – Arpit Jul 27 '17 at 11:56

4 Answers4

2

Use %2B instead + symbol

so parameter should be like Dahshshs%2B24

More details Characters and symbols

sasikumar
  • 12,540
  • 3
  • 28
  • 48
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)

Arpit
  • 323
  • 4
  • 13
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

Antonio314
  • 61
  • 6
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");
Debanjan
  • 2,817
  • 2
  • 24
  • 43