Java URL Query Split splitQuery(URL url)

Here you can find the source of splitQuery(URL url)

Description

split Query

License

Open Source License

Declaration

public static Map<String, List<String>> splitQuery(URL url) throws UnsupportedEncodingException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Main {
    public static Map<String, List<String>> splitQuery(URL url) throws UnsupportedEncodingException {
        final Map<String, List<String>> query_pairs = new LinkedHashMap<String, List<String>>();
        final String[] pairs = url.getQuery().split("&");
        for (String pair : pairs) {
            final int idx = pair.indexOf("=");
            final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
            if (!query_pairs.containsKey(key)) {
                query_pairs.put(key, new LinkedList<String>());
            }/*from   ww  w . j  av a  2  s.  c  o m*/
            final String value = idx > 0 && pair.length() > idx + 1
                    ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8")
                    : null;
            query_pairs.get(key).add(value);
        }
        return query_pairs;
    }
}

Related

  1. splitQuery(String query)
  2. splitQuery(String query)
  3. splitQuery(String queryString)
  4. splitQuery(String url)
  5. splitQuery(URL url)