Skip to content

Instantly share code, notes, and snippets.

@yanhua365
Created January 20, 2014 05:26
Show Gist options
  • Save yanhua365/8515289 to your computer and use it in GitHub Desktop.
Save yanhua365/8515289 to your computer and use it in GitHub Desktop.
用Java解析HTTP Request的params参数,生成一个Map的代码。http://stackoverflow.com/questions/13592236/parse-the-uri-string-into-name-value-collection-in-java
public static Map<String, String> splitQuery(URL url) throws UnsupportedEncodingException {
Map<String, String> query_pairs = new LinkedHashMap<String, String>();
String query = url.getQuery();
String[] pairs = query.split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
}
return query_pairs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment