Java URL Create constructURLQueryString(Map urlParameters)

Here you can find the source of constructURLQueryString(Map urlParameters)

Description

This method is the opposite for extractURLParameters(String url).

License

Apache License

Parameter

Parameter Description
urlParameters Map of parameters to use in query string construction.

Return

URL-encoded query string.

Declaration

public static String constructURLQueryString(Map<String, String> urlParameters) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w.j  a  v  a2s .c  o m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.Map;

public class Main {
    /**
     * This method is the opposite for <code>extractURLParameters(String url)</code>.
     * It takes a map of parameters, performs URL-encoding of each and assembles them
     * into a query string.
     * 
     * The query string then can be added to the <code>URL</code> object by using standard
     * Java API. 
     * 
     * @param urlParameters Map of parameters to use in query string construction.
     * @return URL-encoded query string.
     */
    public static String constructURLQueryString(Map<String, String> urlParameters) {
        if (urlParameters != null) {
            StringBuilder queryString = new StringBuilder();

            // iterate through all parameters and reconstruct the query string
            for (Map.Entry<String, String> parameter : urlParameters.entrySet()) {
                if (queryString.length() > 0)
                    queryString.append("&"); // parameter separator
                queryString.append(urlEncodeQuery(parameter.getKey()) + "=" + urlEncodeQuery(parameter.getValue()));
            }

            return (queryString.toString());
        } else {
            return (null);
        }
    }

    /**
     * Prepares the string to serve as a part of url query to the server.
     * @param query The string that needs URL encoding.
     * @return URL encoded string that can be inserted into the request URL.
     */
    public static String urlEncodeQuery(String query) {
        // "fast exit" - if null supplied, just return an empty string;
        // this is because in the URLs we have "q=", rather than "q=null" - this will cater for such cases
        if (query == null)
            return ("");

        // encode the query
        String strRes = "";
        try {
            strRes = URLEncoder.encode(query, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // do nothing
        }

        return (strRes);
    }
}

Related

  1. concatenateURL(final URL url, final String query)
  2. concatenateURL(URL url, String query)
  3. concatUrl(final URL baseUrl, final String extraPath)
  4. concaturl(final URL p_base, final String p_string)
  5. constructURL(URL base, String url, boolean stripRef)
  6. constructURLString(Map parameters)
  7. convertToURL(String host)
  8. convertToURL(String url)
  9. convertToUrl(String[] paths)