Java URL Parameter Builder convertHttpParameters(Map parameters)

Here you can find the source of convertHttpParameters(Map parameters)

Description

Useful method to convert a map of key,value pairs to a String to be used as part of a GET or POST content.

License

Apache License

Parameter

Parameter Description
parameters A Map<String, String> with the parameters to encode.

Return

The String with the parameters encoded.

Declaration

public static String convertHttpParameters(Map<String, String> parameters) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 See AUTHORS file./*from  w w w. ja v a2 s.c om*/
 * 
 * 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.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Set;

public class Main {
    public static String defaultEncoding = "UTF-8";
    public static String nameValueSeparator = "=";
    public static String parameterSeparator = "&";

    /** Useful method to convert a map of key,value pairs to a String to be used as part of a GET or POST content.
     * @param parameters A Map<String, String> with the parameters to encode.
     * @return The String with the parameters encoded. */
    public static String convertHttpParameters(Map<String, String> parameters) {
        Set<String> keySet = parameters.keySet();
        StringBuilder convertedParameters = new StringBuilder();
        for (String name : keySet) {
            convertedParameters.append(encode(name, defaultEncoding));
            convertedParameters.append(nameValueSeparator);
            convertedParameters.append(encode(parameters.get(name), defaultEncoding));
            convertedParameters.append(parameterSeparator);
        }
        if (convertedParameters.length() > 0)
            convertedParameters.deleteCharAt(convertedParameters.length() - 1);
        return convertedParameters.toString();
    }

    private static String encode(String content, String encoding) {
        try {
            return URLEncoder.encode(content, encoding);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

Related

  1. addParameter(Map> map, String pair)
  2. canonicalize(final SortedMap sortedParamMap)
  3. canonicalize(final SortedMap sortedParamMap)
  4. constructParameterString(TreeMap pPostParms)
  5. convertToDataString(Map data)
  6. convertToParameterString(Map parameterMap)
  7. extractParameters(String parameterString)
  8. format(final Map> parameters, final String encoding)