开发者

Does URLEncodedUtils.parse() presever the order?

Does URLEncodedUtils.parse(uri, encoding) return the parameters in the 开发者_JAVA技巧same order mentioned in the URL? And does it return all the values for parameters with duplicate names?


public static void parse (
       final List <NameValuePair> parameters, 
       final Scanner scanner, 
       final String encoding) {
   scanner.useDelimiter(PARAMETER_SEPARATOR);
   while (scanner.hasNext()) {
       final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR);
       if (nameValue.length == 0 || nameValue.length > 2)
           throw new IllegalArgumentException("bad parameter");

        final String name = decode(nameValue[0], encoding);
        String value = null;
       if (nameValue.length == 2)
           value = decode(nameValue[1], encoding);
       parameters.add(new BasicNameValuePair(name, value));
   }
}

As you can see in the parse-function, it preserves the order of the parameters in the URL. But this is an implementation detail and not documented, so it can change without warning. You really should not rely on that behavior in your code.


Since it returns a list, it should return the name-value pairs in order and should also return duplicates.

However, you shouldn't rely on the order of the parameters but rather access them via their names. Duplicates would be bad anyways, i.e. what would a lang=en&lang=de mean? If you need a list you'd better do something like languages=en,de or provide better parameter names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜