通过HttpClient获取响应内容字符集Java认证考试
文章作者 100test 发表时间 2010:01:01 15:50:42
来源 100Test.Com百考试题网
/**
*正则匹配
* @param s
* @param pattern
* @return
*/
public boolean matcher(String s, String pattern) { Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE Pattern.UNICODE_CASE). Matcher matcher = p.matcher(s). if (matcher.find()) {
return true.
} else {
return false.
}
}
/** *//**
*获取Response内容字符集
*
* @param response
* @return
*/
public String getContentCharset(HttpResponse response) {
String charset = "ISO_8859-1".
Header header = response.getEntity().getContentType(). if (header != null) { String s = header.getValue(). if (matcher(s, "(charset)\\s?=\\s?(utf-?8)")) {
charset = "utf-8".
} else if (matcher(s, "(charset)\\s?=\\s?(gbk)")) {
charset = "gbk".
} else if (matcher(s, "(charset)\\s?=\\s?(gb2312)")) {
charset = "gb2312".http://ks.100test.com
}
}
return charset.
}
编辑特别推荐:
指点一下:到底该不该去考JAVA认证?