Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.UnsupportedEncodingException; import java.net.URLEncoder; public class Main { private static final String addURIParamIfExists(String url, String key, String value) { if (url == null || url.equals("")) { return url; } if (!url.contains("?")) { url += "?"; } if (!(url.endsWith("&") || url.endsWith("?"))) { url += "&"; } url += key + "=" + encodeURIComponent(value); return url; } private static final String addURIParamIfExists(String url, String key, Integer value) { return (value == null) ? url : addURIParamIfExists(url, key, String.valueOf(value)); } private static final String encodeURIComponent(final String s) { String result; try { result = URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20").replaceAll("\\%21", "!") .replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")") .replaceAll("\\%7E", "~"); } catch (UnsupportedEncodingException e) { result = s; } return result; } }