如何找到mimetype的响应 [英] How to find mimetype of response
问题描述
我正在处理使用Apache HTTP客户端发出的GET请求(v4-最新版本;而不是旧版本的v3)...
I am working with a GET request made using Apache HTTP client (v4- the latest version; not the older v3)...
我如何获得响应的mimetype?
How do I obtain the mimetype of the response?
在apache http客户端的旧v3中,使用以下代码获取mime类型 -
In the older v3 of apache http client, mime type was obtained using following code--
String mimeType = response.getMimeType();
如何使用apache http客户端的v4获取mimetype?
How do I get the mimetype using v4 of apache http client?
推荐答案
内容类型HTTP标头应该为您提供mime类型信息:
A "Content-type" HTTP header should give you mime type information:
Header contentType = response.getFirstHeader("Content-Type");
或
Header contentType = response.getEntity().getContentType();
然后你可以提取mime类型本身,因为内容类型也可能包含编码。
Then you can extract mime type itself as the content-type may include encoding as well.
String mimeType = contentType.getValue().split(";")[0].trim();
当然,在获取标题值之前不要忘记进行空值检查(如果是服务器不发送内容类型标题。
Of course, don't forget about null-check before getting value of the header (in case the content-type header is not sent by server).
这篇关于如何找到mimetype的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!