如何从响应中查找HTTP媒体类型(MIME类型)?
使用 Apache HTTP Client v4 发出 GET
请求时,如何获取响应媒体类型(正式的 MIME 类型)?
使用 Apache HTTP Client v3,通过以下方式获取 MIME 类型:
String mimeType = response.getMimeType();
How do I get the media type using Apache HTTP Client v4?
While issuing a GET
request using Apache HTTP Client v4, how do I obtain the response media type (formally MIME type)?
Using Apache HTTP Client v3, the MIME type was obtained with:
String mimeType = response.getMimeType();
How do I get the media type using Apache HTTP Client v4?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要从响应中获取内容类型,您可以使用 ContentType 类。
使用此类,您可以轻松提取 mime type:
或 charset:
To get content type from response you can use ContentType class.
Using this class you can easily extract mime type:
or charset:
“Content-type”HTTP 标头应该为您提供 mime 类型信息:
或 as
然后您可以提取 mime 类型本身,因为内容类型也可能包括编码。
当然,在获取标头值之前不要忘记进行空检查(以防服务器未发送内容类型标头)。
A "Content-type" HTTP header should give you mime type information:
or as
Then you can extract mime type itself as the content-type may include encoding as well.
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).
请注意 Apache 的 ContentType 将 SVG 视为
application/svg+xml
,而不是 IANA 定义image/svg+xml
,这似乎是一个不正确的分类。虽然这个答案没有直接回答这个问题,但它提供了一种通过使用 Java 的 HTTP 客户端来使用 Apache 的 HTTP 客户端的替代方法。另外,示例代码:
HEAD
请求,而不是GET
请求,这是一个更轻量级的操作;MediaType
枚举而不是字符串来向媒体类型添加强类型;并ContentType
未定义的媒体类型,尤其是图像。言归正传,这里有一些可能会有所帮助的 Java 源文件。
MediaType
基本枚举对 IANA 媒体类型进行编码。如果您添加更多官方编码,请更新此答案,以便所有人受益。请注意,R Markdown、R XML 和 YAML 尚未正式定义,因此您可能希望删除它们。
MediaTypeExtensions
不同的文件扩展名映射到不同的媒体类型。扩展名到
MediaType
的映射并不一定意味着内容与预期的媒体类型匹配。应用程序必须注意读取文件头以确定实际的媒体类型。HttpMediaType
最后,我们可以编写一个小型解析器,将内容类型标头转换为 MediaType 值。请注意,
HttpClient
API 本身对标头名称执行区分大小写的比较,因此我们不能使用firstValue
或allValues
等方法,因为我们不这样做不知道服务器是否会返回“Content-Type”或“content-type”。严格来说,这似乎是一个错误,因为 RFC-2616< /a> 表明消息头不区分大小写。Note that Apache's ContentType treats SVG as
application/svg+xml
, rather than the IANA-definedimage/svg+xml
, which appears to be an incorrect categorization.Although this answer doesn't answer the question, directly, it provides an alternative to using Apache's HTTP Client by using Java's HTTP Client. Additionally, the example code:
HEAD
request, rather than aGET
request, which is a lighter operation;MediaType
enum instead of a string; andContentType
.Without further ado, here are a few Java source files that may prove helpful.
MediaType
The base enumeration encodes IANA media types. If you add more official encodings, please update this answer so that all may benefit. Note that R Markdown, R XML, and YAML are not defined, officially, so you may wish to remove them.
MediaTypeExtensions
Different file name extensions map to various media types. The mapping of extensions to
MediaType
does not necessarily mean that the content matches the expected media type. Applications must take care to read the file headers to determine the actual media type.HttpMediaType
Finally, we can write a tiny parser that converts the content-type header into a
MediaType
value. Note that theHttpClient
API itself performs a case-sensitive comparison against the header name, so we cannot use methods likefirstValue
orallValues
because we don't know whether the server will return "Content-Type" or "content-type". Strictly speaking, this appears to be a bug because RFC-2616 states that message headers are not case-sensitive.