Java URL Query Split splitQuery(String url)

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

Description

split Query

License

Open Source License

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static Map<String, String> splitQuery(String url) throws UnsupportedEncodingException {
        URL myURL = null;//from w  ww  . j  a  v a 2 s .  c  o m

        try {
            myURL = new URL(url);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Map<String, String> query_pairs = new LinkedHashMap<String, String>();
        String query = myURL.getQuery();
        String[] pairs = query.split("&");
        for (String pair : pairs) {
            int idx = pair.indexOf("=");
            query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"),
                    URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
        }
        return query_pairs;
    }
}

Related

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