Here you can find the source of addQueryParamsToUri(String uri, Map
public static String addQueryParamsToUri(String uri, Map<String, String> queryParams)
//package com.java2s; /*// w ww.j a va 2 s. co m * Copyright 2016 Red Hat, Inc. and/or its affiliates * and other contributors as indicated by the @author tags. * * 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.URLEncoder; import java.util.LinkedHashMap; import java.util.Map; public class Main { public static String addQueryParamsToUri(String uri, String... queryParams) { if (queryParams == null) { return uri; } if (queryParams.length % 2 != 0) { throw new RuntimeException("Value missing for query parameter: " + queryParams[queryParams.length - 1]); } Map<String, String> params = new LinkedHashMap<>(); for (int i = 0; i < queryParams.length; i += 2) { params.put(queryParams[i], queryParams[i + 1]); } return addQueryParamsToUri(uri, params); } public static String addQueryParamsToUri(String uri, Map<String, String> queryParams) { if (queryParams.size() == 0) { return uri; } StringBuilder query = new StringBuilder(); for (Map.Entry<String, String> params : queryParams.entrySet()) { try { if (query.length() > 0) { query.append("&"); } query.append(params.getKey()).append("=").append(URLEncoder.encode(params.getValue(), "utf-8")); } catch (Exception e) { throw new RuntimeException( "Failed to encode query params: " + params.getKey() + "=" + params.getValue()); } } return uri + (uri.indexOf("?") == -1 ? "?" : "&") + query; } }