html网页的内容大致如下:
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
无论是哪一句,得到的contentType的值都是text/html,而不包含后面的charset=gb2312,这是为什么?
我注意到一个问题,content="text/html; charset=gb2312"这里,在;和charset之间有一个空格,是否因为这个空格的缘故,而不能得到content-type的准确值
还有没有其他的办法可以得到页面的编码字符集?
可以用 HttpClient jar包
GetMethod method = new GetMethod(url);
String charset = null;
if(StringUtils.isEmpty(encoding)){
charset = method.getRequestCharSet();
if("ISO-8859-1".equalsIgnoreCase(charset)) //未指定编码时返回ISO-8859-1, 改为默认的GB2312
charset = "GB2312";
}
我用的是:
httpclient的
HttpMethodBase method;
method = new GetMethod(url);
identifyAsDefaultBrowser(method);
charset = method.getResponseCharSet();
String charset = connection.getContentEncoding();