Java URL Query Build getQueryParameters(URL url)

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

Description

Retrieve query parameters map from String representation of url

License

Open Source License

Parameter

Parameter Description
url a parameter

Exception

Parameter Description
UnsupportedEncodingException an exception

Return

- Map with parameters names as map keys and parameters values as map values

Declaration

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

Method Source Code


//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    /**//from w w  w. j a  v  a 2s .com
     * Retrieve query parameters map from String representation of url
     *
     * @param url
     * @return - <code>Map</code> with parameters names as map keys and parameters values as map values
     * @throws UnsupportedEncodingException
     */
    public static Map<String, List<String>> getQueryParameters(URL url) throws UnsupportedEncodingException {
        Map<String, List<String>> params = new HashMap<>();
        String query = url.getQuery();
        if (query != null) {
            for (String param : query.split("&")) {
                String pair[] = param.split("=");
                String key = URLDecoder.decode(pair[0], "UTF-8");
                String value = null;
                if (pair.length > 1) {
                    value = URLDecoder.decode(pair[1], "UTF-8");
                }
                List<String> values = params.get(key);
                if (values == null) {
                    values = new ArrayList<>();
                    params.put(key, values);
                }
                values.add(value);
            }
        }
        return params;
    }
}

Related

  1. getQueryMap(String urlString)
  2. getQueryMap(URL url)
  3. getQueryParameter(String query, String key, String encoding)
  4. getQueryParameterMap(String queryString)
  5. getQueryParameters(URL url)
  6. getQueryParameters(URL url)
  7. getQueryParametersFromUrl(String url)
  8. getQueryParams(String httpUrl)
  9. getQueryParams(String query)