如何找到mimetype的响应
我正在处理使用Apache HTTP客户端发出的GET请求(v4-最新版本;而不是旧版本的v3)…
我如何获得响应的mimetype?
在apache http客户端的旧v3中,使用以下代码获取mime类型 –
String mimeType = response.getMimeType();
如何使用apache http客户端的v4获取mimetype?
“Content-type”HTTP标头应该为您提供mime类型信息:
Header contentType = response.getFirstHeader("Content-Type");
或者作为
Header contentType = response.getEntity().getContentType();
然后你可以提取mime类型本身,因为内容类型也可能包括编码。
String mimeType = contentType.getValue().split(";")[0].trim();
当然,在获取标头的值之前不要忘记进行空检查(如果服务器没有发送内容类型标头)。
要从响应中获取内容类型,您可以使用ContentType类。
HttpEntity entity = response.getEntity(); ContentType contentType; if (entity != null) contentType = ContentType.get(entity);
使用此类可以轻松提取mime类型:
String mimeType = contentType.getMimeType();
或charset:
Charset charset = contentType.getCharset();