Created
July 21, 2011 15:55
-
-
Save aembleton/1097503 to your computer and use it in GitHub Desktop.
Converts a Map into URLEncoded data that can be used in a POST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.blerg; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLEncoder; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class MapToData { | |
private static final String UTF8 = "UTF-8"; | |
private static final Logger log = LoggerFactory.getLogger(MapToData.class); | |
public static String mapToData(Map<String, String> map) { | |
boolean first = true; | |
String data = ""; | |
for (Entry<String, String> entry : map.entrySet()) { | |
if (first) { | |
first = false; | |
} else { | |
data += "&"; | |
} | |
try { | |
data += URLEncoder.encode(entry.getKey(), UTF8) + "=" + URLEncoder.encode(entry.getValue(), UTF8); | |
} catch (UnsupportedEncodingException e) { | |
log.error("URLEncoding failed, with either " + entry.getKey() + " and/or " + entry.getValue(), e); | |
} | |
} | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment