@Test public void testGetParams() { assertEquals("null map should return an empty string", "", IOHelper.getParams(null)); Map<String, String> params = new LinkedHashMap<String, String>(); assertEquals("Empty map should return an empty string", "", IOHelper.getParams(params)); params.put("key1", "foo"); assertEquals("Key and value should be separated with a '='", "key1=foo", IOHelper.getParams(params)); params.put("key2", "bar"); assertEquals("Values should be separated with a '&'", "key1=foo&key2=bar", IOHelper.getParams(params)); params.remove("key2"); params.put("key1", "baz+qux"); assertEquals("Values should be URL encoded", "key1=baz%2Bqux", IOHelper.getParams(params)); params.remove("key1"); params.put("foo+bar", "baz+qux"); assertEquals("Keys should be URL encoded", "foo%2Bbar=baz%2Bqux", IOHelper.getParams(params)); params.remove("foo+bar"); params.put("key1", "baz qux"); assertEquals("Space characters are replaced by '%20' in encoding", "key1=baz%20qux", IOHelper.getParams(params)); }
public static String getParams(Map<String, String> kvps) { if (kvps == null || kvps.isEmpty()) { return ""; } final StringBuilder sb = new StringBuilder(); boolean first = true; for (Map.Entry<String, String> entry : kvps.entrySet()) { final String key = entry.getKey(); final String value = entry.getValue(); if (key == null || key.isEmpty()) { continue; } if (value == null) { continue; } final String keyEnc = urlEncodePayload(key); final String valueEnc = urlEncodePayload(value); if (!first) { sb.append('&'); } sb.append(keyEnc).append('=').append(valueEnc); first = false; } return sb.toString(); }
IOHelper { public static String getParams(Map<String, String> kvps) { if (kvps == null || kvps.isEmpty()) { return ""; } final StringBuilder sb = new StringBuilder(); boolean first = true; for (Map.Entry<String, String> entry : kvps.entrySet()) { final String key = entry.getKey(); final String value = entry.getValue(); if (key == null || key.isEmpty()) { continue; } if (value == null) { continue; } final String keyEnc = urlEncodePayload(key); final String valueEnc = urlEncodePayload(value); if (!first) { sb.append('&'); } sb.append(keyEnc).append('=').append(valueEnc); first = false; } return sb.toString(); } }
IOHelper { public static String getParams(Map<String, String> kvps) { if (kvps == null || kvps.isEmpty()) { return ""; } final StringBuilder sb = new StringBuilder(); boolean first = true; for (Map.Entry<String, String> entry : kvps.entrySet()) { final String key = entry.getKey(); final String value = entry.getValue(); if (key == null || key.isEmpty()) { continue; } if (value == null) { continue; } final String keyEnc = urlEncodePayload(key); final String valueEnc = urlEncodePayload(value); if (!first) { sb.append('&'); } sb.append(keyEnc).append('=').append(valueEnc); first = false; } return sb.toString(); }  }
IOHelper { public static String getParams(Map<String, String> kvps) { if (kvps == null || kvps.isEmpty()) { return ""; } final StringBuilder sb = new StringBuilder(); boolean first = true; for (Map.Entry<String, String> entry : kvps.entrySet()) { final String key = entry.getKey(); final String value = entry.getValue(); if (key == null || key.isEmpty()) { continue; } if (value == null) { continue; } final String keyEnc = urlEncodePayload(key); final String valueEnc = urlEncodePayload(value); if (!first) { sb.append('&'); } sb.append(keyEnc).append('=').append(valueEnc); first = false; } return sb.toString(); }  static int getConnectionTimeoutMs(); static int getReadTimeoutMs(); static String getMyDomain(); static String readString(InputStream is); static String readString(HttpURLConnection conn); static String readString(HttpURLConnection conn, final String charset); static List<String> readLines(InputStream in); static List<String> readLines(InputStream in, Charset cs); static String readString(InputStream is, String charset); static String readString(InputStream in, Charset cs); static byte[] readBytes(HttpURLConnection conn); static void readBytesTo(HttpURLConnection conn, OutputStream out); static byte[] readBytes(InputStream is); static void copy(InputStream in, OutputStream out); static void copy(InputStream in, OutputStream out, long sizeLimit); static HttpURLConnection getConnectionFromProps(final String propertiesPrefix); static HttpURLConnection getConnectionFromProps(final String url, final String propertiesPrefix); static HttpURLConnection getConnection(final String pUrl); static HttpURLConnection getConnection(final String pUrl,
                                                  final String userName, final String password); static HttpURLConnection getConnection(String pUrl,
            String user, String pass, Map<String, String> query); static HttpURLConnection getConnection(String pUrl,
            String user, String pass, Map<String, String> query, Map<String, String> headers); static HttpURLConnection followRedirect(HttpURLConnection conn, int redirectLatch); static HttpURLConnection followRedirect(HttpURLConnection conn,
            String user, String pass, int redirectLatch); static HttpURLConnection followRedirect(HttpURLConnection conn,
            String user, String pass, Map<String, String> query, int redirectLatch); static HttpURLConnection followRedirect(HttpURLConnection conn,
            String user, String pass, Map<String, String> query,
            Map<String, String> headers, int redirectLatch); static InputStream debugResponse(final InputStream in); static String getCharset(final HttpURLConnection con); static String getCharset(final HttpURLConnection con, final String defaultCharset); static void setupBasicAuth(final HttpURLConnection con,final String userName, final String password); static void writeToConnection(final HttpURLConnection con,
                                         final String postData); static void writeToConnection(final HttpURLConnection con,
                                         final byte[] bytes); static String getURL(final String pUrl); static String getURL(final String pUrl, final String charset); static String getURL(final String pUrl,
                                final Map<String, String> headers); static String getURL(final String pUrl,
                                final Map<String, String> headers, final String charset); static String getURL(final HttpURLConnection con,
                                final Map<String, String> headers, final String charset); static String getURL(final String pUrl, final String userName, final String password ); static String getURL(final String pUrl, final String userName, final String password,
                                final Map<String, String> headers); static String getURL(final String pUrl,final String userName, final String password,
                                final Map<String, String> headers, final String charset); static void writeHeaders(final HttpURLConnection con,
                                    final Map<String, String> headers); static void writeHeader(final HttpURLConnection con,
                                    final String header, final String value); static void setContentType(final HttpURLConnection con,
                                   final String value); static String encode64(String in); static String decode64(String in); static HttpURLConnection postForm(String url, Map<String, String> keyValuePairs); static HttpURLConnection post(String url, String contentType,
            byte[] body); static HttpURLConnection post(String url, String contentType,
            ByteArrayOutputStream baos); static HttpURLConnection post(HttpURLConnection conn, String contentType,
            byte[] body); static HttpURLConnection post(HttpURLConnection conn, String contentType,
                                         String body); static HttpURLConnection post(HttpURLConnection conn, String contentType,
            ByteArrayOutputStream baos); static HttpURLConnection put(String url, String contentType, byte[] body); static HttpURLConnection put(HttpURLConnection conn, String contentType, byte[] body); static void close(final Closeable in); static void trustAllCerts(final HttpURLConnection connection); static void trustAllHosts(final HttpURLConnection connection); static Map<String, List<String>> parseQuerystring(String url); static Map<String, List<String>> parseQuerystring(URL url); static String removeQueryString(String url); static String constructUrl(final String url, Map<String, String> params); static String addQueryString(String url, String queryString); static String fixPath(String url); static String addUrlParam(final String url, String key, String... value); static String createQuerystring(Map<String, List<String>> kvps); static String getParams(Map<String, String> kvps); static String getParamsMultiValue(Map<String, String[]> kvps); static String urlEncodePayload(String s); static String urlEncode(String s); @Deprecated static InputStream getInputStream(HttpURLConnection conn); static void closeSilently(HttpURLConnection c); static void readFullyIgnoring(InputStream in); static ByteArrayOutputStream gzip(byte[] bytes); static ByteArrayOutputStream ungzip(byte[] cached); }
IOHelper { public static String getParams(Map<String, String> kvps) { if (kvps == null || kvps.isEmpty()) { return ""; } final StringBuilder sb = new StringBuilder(); boolean first = true; for (Map.Entry<String, String> entry : kvps.entrySet()) { final String key = entry.getKey(); final String value = entry.getValue(); if (key == null || key.isEmpty()) { continue; } if (value == null) { continue; } final String keyEnc = urlEncodePayload(key); final String valueEnc = urlEncodePayload(value); if (!first) { sb.append('&'); } sb.append(keyEnc).append('=').append(valueEnc); first = false; } return sb.toString(); }  static int getConnectionTimeoutMs(); static int getReadTimeoutMs(); static String getMyDomain(); static String readString(InputStream is); static String readString(HttpURLConnection conn); static String readString(HttpURLConnection conn, final String charset); static List<String> readLines(InputStream in); static List<String> readLines(InputStream in, Charset cs); static String readString(InputStream is, String charset); static String readString(InputStream in, Charset cs); static byte[] readBytes(HttpURLConnection conn); static void readBytesTo(HttpURLConnection conn, OutputStream out); static byte[] readBytes(InputStream is); static void copy(InputStream in, OutputStream out); static void copy(InputStream in, OutputStream out, long sizeLimit); static HttpURLConnection getConnectionFromProps(final String propertiesPrefix); static HttpURLConnection getConnectionFromProps(final String url, final String propertiesPrefix); static HttpURLConnection getConnection(final String pUrl); static HttpURLConnection getConnection(final String pUrl,
                                                  final String userName, final String password); static HttpURLConnection getConnection(String pUrl,
            String user, String pass, Map<String, String> query); static HttpURLConnection getConnection(String pUrl,
            String user, String pass, Map<String, String> query, Map<String, String> headers); static HttpURLConnection followRedirect(HttpURLConnection conn, int redirectLatch); static HttpURLConnection followRedirect(HttpURLConnection conn,
            String user, String pass, int redirectLatch); static HttpURLConnection followRedirect(HttpURLConnection conn,
            String user, String pass, Map<String, String> query, int redirectLatch); static HttpURLConnection followRedirect(HttpURLConnection conn,
            String user, String pass, Map<String, String> query,
            Map<String, String> headers, int redirectLatch); static InputStream debugResponse(final InputStream in); static String getCharset(final HttpURLConnection con); static String getCharset(final HttpURLConnection con, final String defaultCharset); static void setupBasicAuth(final HttpURLConnection con,final String userName, final String password); static void writeToConnection(final HttpURLConnection con,
                                         final String postData); static void writeToConnection(final HttpURLConnection con,
                                         final byte[] bytes); static String getURL(final String pUrl); static String getURL(final String pUrl, final String charset); static String getURL(final String pUrl,
                                final Map<String, String> headers); static String getURL(final String pUrl,
                                final Map<String, String> headers, final String charset); static String getURL(final HttpURLConnection con,
                                final Map<String, String> headers, final String charset); static String getURL(final String pUrl, final String userName, final String password ); static String getURL(final String pUrl, final String userName, final String password,
                                final Map<String, String> headers); static String getURL(final String pUrl,final String userName, final String password,
                                final Map<String, String> headers, final String charset); static void writeHeaders(final HttpURLConnection con,
                                    final Map<String, String> headers); static void writeHeader(final HttpURLConnection con,
                                    final String header, final String value); static void setContentType(final HttpURLConnection con,
                                   final String value); static String encode64(String in); static String decode64(String in); static HttpURLConnection postForm(String url, Map<String, String> keyValuePairs); static HttpURLConnection post(String url, String contentType,
            byte[] body); static HttpURLConnection post(String url, String contentType,
            ByteArrayOutputStream baos); static HttpURLConnection post(HttpURLConnection conn, String contentType,
            byte[] body); static HttpURLConnection post(HttpURLConnection conn, String contentType,
                                         String body); static HttpURLConnection post(HttpURLConnection conn, String contentType,
            ByteArrayOutputStream baos); static HttpURLConnection put(String url, String contentType, byte[] body); static HttpURLConnection put(HttpURLConnection conn, String contentType, byte[] body); static void close(final Closeable in); static void trustAllCerts(final HttpURLConnection connection); static void trustAllHosts(final HttpURLConnection connection); static Map<String, List<String>> parseQuerystring(String url); static Map<String, List<String>> parseQuerystring(URL url); static String removeQueryString(String url); static String constructUrl(final String url, Map<String, String> params); static String addQueryString(String url, String queryString); static String fixPath(String url); static String addUrlParam(final String url, String key, String... value); static String createQuerystring(Map<String, List<String>> kvps); static String getParams(Map<String, String> kvps); static String getParamsMultiValue(Map<String, String[]> kvps); static String urlEncodePayload(String s); static String urlEncode(String s); @Deprecated static InputStream getInputStream(HttpURLConnection conn); static void closeSilently(HttpURLConnection c); static void readFullyIgnoring(InputStream in); static ByteArrayOutputStream gzip(byte[] bytes); static ByteArrayOutputStream ungzip(byte[] cached); static final String HEADER_AUTHORIZATION; static final String HEADER_CONTENTTYPE; static final String HEADER_USERAGENT; static final String HEADER_REFERER; static final String HEADER_ACCEPT; static final String HEADER_ACCEPT_CHARSET; static final String HEADER_ACCEPT_ENCODING; static final String CHARSET_UTF8; static final String DEFAULT_CHARSET; static final Charset DEFAULT_CHARSET_CS; static final String CONTENTTYPE_FORM_URLENCODED; static final String CONTENT_TYPE_JSON; static final String CONTENT_TYPE_XML; static final String ENCODING_GZIP; }