HttpClient为4.0版本 Code/** *//** * 正则匹配 * @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"; } } return charset; } 转载于:https://www.cnblogs.com/heroking2000/archive/2009/10/17/1584842.html