httpclient有没有一种方法可以获取带有HEAD请求的页面字符集? [英] with httpclient is there a way to get the character set of the page with a HEAD request?

查看:149
本文介绍了httpclient有没有一种方法可以获取带有HEAD请求的页面字符集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用httpclient库执行基本的HEAD请求.我很好奇我如何能够获得apache返回的字符集,例如:utf-8,iso-8859-1等. 谢谢!

I'm doing a basic HEAD request using httpclient library. I'm curious how I'd be able to get the character set that apache returns E.g.: utf-8, iso-8859-1, etc... thanks!

  HttpParams httpParams = new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(httpParams, 2000);
  HttpConnectionParams.setSoTimeout(httpParams, 2000);

  DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
  httpclient.getParams().setParameter("http.useragent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

  HttpContext localContext = new BasicHttpContext();
  httpget = new HttpHead(url); 

  HttpResponse response = httpclient.execute(httpget, localContext);

  this.sparrowResult.statusCode = response.getStatusLine().getStatusCode();

工作结果已更新

Header contentType = response.getFirstHeader("Content-Type");
String charset= contentType.getValue();

推荐答案

在HTTP 1.1中,字符集位于Content-Type标头中

in HTTP 1.1, the character set is in the Content-Type Header

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8

所以应该埋在里面

HttpResponse.Headers

所以,这应该起作用

HttpResponse.Headers.["Content-Type"]

**没有测试过,但是您知道了

** didnt test this, but you get the idea

这篇关于httpclient有没有一种方法可以获取带有HEAD请求的页面字符集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆