【研究】httpclient 个别汉字乱码
本帖最后由 rywaqpf 于 2012-05-03 17:55:33 编辑 发现httpclient访问网页时,汉字“”是乱码,其他的汉字都正常。
不过,访问百度网页时正常,访问百度知道却出现乱码问题。
怎么才能不出现乱码,请各位帮忙看下,先谢过,可加分。
环境:jdk1.5.0_14,httpclient-4.1.2.jar
附上代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class Test {
public static void main(String[] args) {
try {
String[] urlAry = new String[]{
//百度网页,""字正常
"http://www.baidu.com/s?cl=3&wd=%CB%DE%88%AC%D6%B4",
//百度知道,""字乱码
"http://zhidao.baidu.com/q?word=%CB%DE%88%AC%D6%B4&lm=0&fr=search&ct=17&pn=0&tn=ikaslist&rn=10"
};
for (String queryURL : urlAry) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(queryURL);
HttpResponse response = client.execute(httpget);
HttpEntity entity = response.getEntity();
String returnText = EntityUtils.toString(entity,"gb2312");
//网页代码
// System.out.println(returnText);
//通过正则表达式,摘出要比较的部分
getTextByRule(".*?(宿.*?执).*",returnText);
client.getConnectionManager().shutdown();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void getTextByRule(String parttern, String str){
Pattern p = Pattern.compile(parttern);
Matcher matcher = p.matcher(str);
if(matcher.find()) {
System.out.println(matcher.group(1));
}
}
}
[解决办法]
String returnText = EntityUtils.toString(entity,"GBK");
[解决办法]
有没有发现实际上这句中的编码根本没有起作用,defaultCharset只有在entity中未提供编码时才会起作用
String returnText = EntityUtils.toString(entity,"gb2312");
编码随便改,即使改成123也不会对结果有任何影响
public static String toString(HttpEntity entity,
String defaultCharset)
throws IOException,
ParseException
Get the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.
两个结果的差异是由各自的URL中带来的编码决定的,前者是GBK,后者是GB2312
因字在GB2312中无编码,在GBK中是88AC(十进制:34988),所以后者无法呈现。
[解决办法]
++
[解决办法]
对于浏览器来说,首先使用 HTTP 响应头中的 Content-Type 来决定使用的编码格式。如果响应头中没有 Content-Type 或者 Content-Type 中没有指定 charset 时则会采用 HTML meta 中的 Content-Type,如果都没有,就使用浏览器默认的编码格式。
对于浏览器来说,哪怕是设置成 GB2312 的编码格式,GBK 中的字符照样能正常显示,因为对于浏览器来说用 GB2312 就是用 GBK,呵呵。GBK 是兼容 GB2312 的。