欢迎来到Heck's Blog,专业承接拿站、企业建站、仿站、网上商城架构、门户网站搭建、空间域名注册、软件定制等项目。关注网络安全,因为专注,所以专业,懂得放弃,才能收获。有事请发邮件至i@heckjj.com,请记住本站网址:http://www.heckjj.com,多谢。
4月11
因为在最近项目中需要解析日志中的 URL 的参数,所以我对比了一下五种不同 的 URL 参数解析方法的性能。
URL 参数解析方法:
httpclient org.apache.http.client.utils.URLEncodedUtils
URLEncodedUtils.parse(query, Charset.forName("UTF-8"));
jettyUtil org.eclipse.jetty.util.UrlEncoded
MultiMap values = new MultiMap();
UrlEncoded.decodeTo(query, values, "UTF-8", 1000);
tomcat org.apache.catalina.util.RequestUtil
Map values = new HashMap();
RequestUtil.parseParameters(values, query, "UTF-8");
regex 正则表达式
String u = URLDecoder.decode(url, "UTF-8");
for (String s : parameters) {
Pattern p = Pattern.compile(s + "=([^&]*)(&|$)");
Matcher m = p.matcher(u);
if (m.find()) {
m.group(1);
}
}
split 使用String 的split 方法对 URL 进行分割,然后用equals 匹配对应的 参数
String u = URLDecoder.decode(url, "UTF-8");
for (String s : parameters) {
String[] a = new String[100];
if (u.indexOf(s) != -1) {
a = (u.substring(u.indexOf(s))).split("&");
a[0].split("=");
}
}
前三者是 httpclient, jetty, tomcat 使用的 URL 解析工具。Split 方法是最简单 也是最直观的解析方法,regex 则使用了正则表达式去匹配参数。
URL 参数解析方法:
httpclient org.apache.http.client.utils.URLEncodedUtils
URLEncodedUtils.parse(query, Charset.forName("UTF-8"));
jettyUtil org.eclipse.jetty.util.UrlEncoded
MultiMap
UrlEncoded.decodeTo(query, values, "UTF-8", 1000);
tomcat org.apache.catalina.util.RequestUtil
Map
RequestUtil.parseParameters(values, query, "UTF-8");
regex 正则表达式
String u = URLDecoder.decode(url, "UTF-8");
for (String s : parameters) {
Pattern p = Pattern.compile(s + "=([^&]*)(&|$)");
Matcher m = p.matcher(u);
if (m.find()) {
m.group(1);
}
}
split 使用String 的split 方法对 URL 进行分割,然后用equals 匹配对应的 参数
String u = URLDecoder.decode(url, "UTF-8");
for (String s : parameters) {
String[] a = new String[100];
if (u.indexOf(s) != -1) {
a = (u.substring(u.indexOf(s))).split("&");
a[0].split("=");
}
}
前三者是 httpclient, jetty, tomcat 使用的 URL 解析工具。Split 方法是最简单 也是最直观的解析方法,regex 则使用了正则表达式去匹配参数。





