Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.Map; public class Main { private final static String sortedParams(Map<String, String> params) throws UnsupportedEncodingException { String vals[] = new String[params.size()]; int idx = 0; for (String key : params.keySet()) { vals[idx++] = key; } Arrays.sort(vals); StringBuilder sb = new StringBuilder(); for (int i = 0; i < vals.length; i++) { if (i > 0) { sb.append("&"); } sb.append(vals[i]); sb.append("="); sb.append(encode(params.get(vals[i]))); } return sb.toString(); } final static String encode(String s) throws UnsupportedEncodingException { byte buf[] = s.getBytes("utf-8"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < buf.length; i++) { int cur = (buf[i] & 0xff); if (isReserved(cur)) { sb.append("%"); if (cur < 0x10) { sb.append("0"); } sb.append(Integer.toHexString(cur).toUpperCase()); } else { sb.append((char) (cur)); } } return sb.toString(); } private final static boolean isReserved(int v) { if (((v >= 'a') && (v <= 'z')) || ((v >= 'A') && (v <= 'Z')) || ((v >= '0') && (v <= '9')) || (v == '-') || (v == '_') || (v == '.') || (v == '~')) { return false; } else { return true; } } }