Java URL Query Build getQueryParams(String httpUrl)

Here you can find the source of getQueryParams(String httpUrl)

Description

get Query Params

License

Apache License

Declaration

public static Map<String, String> getQueryParams(String httpUrl) 

Method Source Code

//package com.java2s;
/*//  www.  j av  a2  s  .  c  o  m
 * Copyright 2012-2015, the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Main {
    public static Map<String, String> getQueryParams(String httpUrl) {
        Map<String, String> params = new HashMap<String, String>();
        if (httpUrl == null) {
            return params;
        }

        URL url = null;
        try {
            url = new URL(httpUrl);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

        String query = url.getQuery();
        if (query == null) {
            return params;
        }

        StringTokenizer tokenizer = new StringTokenizer(query, "&");
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            int index = token.indexOf("=");
            params.put(token.substring(0, index).trim(), token.substring(index + 1).trim());
        }

        return params;
    }
}

Related

  1. getQueryParameterMap(String queryString)
  2. getQueryParameters(URL url)
  3. getQueryParameters(URL url)
  4. getQueryParameters(URL url)
  5. getQueryParametersFromUrl(String url)
  6. getQueryParams(String query)
  7. getQueryParams(String s)
  8. getQueryParams(String url)
  9. getQueryString(final Map parameters)