我必须在将Content-Type头值传递给HTTP请求之前验证它。

Content-Type的所有可能值是否有一个特定的列表?

否则,是否有一种方法可以在HTTP请求中使用内容类型之前验证它?


当前回答

你可以在这里找到所有的内容类型: http://www.iana.org/assignments/media-types/media-types.xhtml

最常见的类型有:

Type application: application/java-archive application/EDI-X12 application/EDIFACT application/javascript application/octet-stream application/ogg application/pdf application/xhtml+xml application/x-shockwave-flash application/json application/ld+json application/xml application/zip application/x-www-form-urlencoded Type audio: audio/mpeg audio/x-ms-wma audio/vnd.rn-realaudio audio/x-wav Type image: image/gif image/jpeg image/png image/tiff image/vnd.microsoft.icon image/x-icon image/vnd.djvu image/svg+xml Type multipart: multipart/mixed multipart/alternative multipart/related (using by MHTML (HTML mail).) multipart/form-data Type text: text/css text/csv text/html text/javascript (obsolete) text/plain text/xml Type video: video/mpeg video/mp4 video/quicktime video/x-ms-wmv video/x-msvideo video/x-flv video/webm Type vnd: application/vnd.android.package-archive application/vnd.oasis.opendocument.text application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.presentation application/vnd.oasis.opendocument.graphics application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/vnd.ms-powerpoint application/vnd.openxmlformats-officedocument.presentationml.presentation application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document application/vnd.mozilla.xul+xml

其他回答

你可以在这里找到所有的内容类型: http://www.iana.org/assignments/media-types/media-types.xhtml

最常见的类型有:

Type application: application/java-archive application/EDI-X12 application/EDIFACT application/javascript application/octet-stream application/ogg application/pdf application/xhtml+xml application/x-shockwave-flash application/json application/ld+json application/xml application/zip application/x-www-form-urlencoded Type audio: audio/mpeg audio/x-ms-wma audio/vnd.rn-realaudio audio/x-wav Type image: image/gif image/jpeg image/png image/tiff image/vnd.microsoft.icon image/x-icon image/vnd.djvu image/svg+xml Type multipart: multipart/mixed multipart/alternative multipart/related (using by MHTML (HTML mail).) multipart/form-data Type text: text/css text/csv text/html text/javascript (obsolete) text/plain text/xml Type video: video/mpeg video/mp4 video/quicktime video/x-ms-wmv video/x-msvideo video/x-flv video/webm Type vnd: application/vnd.android.package-archive application/vnd.oasis.opendocument.text application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.presentation application/vnd.oasis.opendocument.graphics application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/vnd.ms-powerpoint application/vnd.openxmlformats-officedocument.presentationml.presentation application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document application/vnd.mozilla.xul+xml

如果您正在使用jaxrs或任何其他工具,那么将会有一个名为mediatype的类。在发送请求之前使用用户拦截器,并将其与此进行比较。

我的目标是覆盖可能的“内容类型”值的子集,你的问题似乎集中在识别已知的内容类型上。

@Jeroen RFC 1341参考很棒,但对于IANA保存的相当详尽的列表来说 官方注册的媒体类型的网页在这里。

RFC 1341定义:

In the Extended BNF notation of RFC 822, a Content-Type header field value is defined as follows: Content-Type := type "/" subtype *[";" parameter] type := "application" / "audio" / "image" / "message" / "multipart" / "text" / "video" / x-token x-token := < The two characters "X-" followed, with no intervening white space, by any token > subtype := token parameter := attribute "=" value attribute := token value := token / quoted-string token := 1*<any CHAR except SPACE, CTLs, or tspecials> tspecials := "(" / ")" / "<" / ">" / "@" ; Must be in / "," / ";" / ":" / "" / <"> ; quoted-string, / "/" / "[" / "]" / "?" / "." ; to use within / "=" ; parameter values

以及一个可以跟随它的已知MIME类型的列表(或者,正如Joe所说,IANA源)。

正如您所看到的,这个列表太大了,您无法对所有这些进行验证。您所能做的是根据通用格式和type属性进行验证,以确保它是正确的(选项集很小),并假设它后面的内容是正确的(当然,捕获实际使用时可能遇到的任何异常)。

还要注意上面的评论:

如果出于任何原因要使用另一种主要类型,则必须给它一个以“X-”开头的名称,以表明其非标准状态,并避免与未来的正式名称发生任何潜在冲突。

你会注意到很多HTTP请求/响应都包含一个自定义的X报头,在验证类型时要记住这一点。