15.4 Content-Type:MIME 类型和字符集

本文介绍了HTTP中的多部分媒体类型,包括多部分表格提交和多部分范围响应的应用场景及实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • Content-Type 首部字段说明了实体主体的 MIME 类型。(在HEAD请求中,Content-Type 说明如果请求是GET时,将要发送的主体的类型。)
  • MIME 介绍见1.2.1 Web 资源——媒体类型(MIME)
  • 注意:Content-Type 首部说明的是原始实体主体的媒体类型。如果实体经过内容编码的话,Content-Type 首部说明的仍是编码之前的实体主体的类型。
  • Content-Type 首部还支持可选的参数来进一步说明内容的类型。charset (字符集)参数就是个例子,它说明把实体中的比特转换为文本文件中的字符的方法:
    Content-Type: text/html; charset=iso-8859-4

1. 多部分媒体类型

  • MIME 中的 multipart(多部分)电子邮件报文中包含多个报文,它们合在一起作为单一的复杂报文发送。每一部分都是独立的,有各自的描述其内容的集;不同的部分之间用分界字符串连接在一起。
  • HTTP 也支持多部分主体。不过,通常只用在下列两种情形之一:提交填写好的表格,或是作为承载若干文档片段的范围响应。

2. 多部分表格提交

  • 当提交填写的 HTTP 表格时,变长的文本字段和上传的对象都作为多部分主体里面独立的部分发送,这样表格中就可以填写各种不同类型和长度的值。
  • HTTP 使用 Content-Type:multipart/form-data 或 Content-Type:multipart/ mixed 这样的首部以及多部分主体来发送这种请求,比如:
    Content-Type: multipart/form-data; boundary=[abcdefghijklmnopqrstuvwxyz]
    其中的 boundary 参数说明了分割主体中不同部分所用的字符串。
  • 下面的例子展示了 multipart/form-data 编码。假设我们有这样的表格:
<form action=”http://server.com/cgi/handleenctype="multipart/form-data" method="post">
    <p>
        What is your name? <input type="text" name="submit-name"><br /> 
        What files are you sending? <input type="file" name="files"><br /> 
        <input type="submit" value="Send"> <input type="reset">
    </p>
</form>
  • 如果用户在文本输入字段中键入 Sally,并选择了文本文件 essayfile.txt,用户 Agent 代理可能会发回下面这样的数据:
Content-Type: multipart/form-data; boundary=AaB03x 
--AaB03x
Content-Disposition: form-data; name="submit-name" Sally
--AaB03x
Content-Disposition: form-data; name="files"; filename="essayfile.txt" 
Content-Type: text/plain
...contents of essayfile.txt...
--AaB03x--
  • 如果用户还选了另一个(图像)文件 imagefile.gif,用户 Agent 代理可能像下面这样构造这个部分:
Content-Type: multipart/form-data; boundary=AaB03x 
--AaB03x
Content-Disposition: form-data; name="submit-name" Sally
--AaB03x
Content-Disposition: form-data; name="files" 
Content-Type: multipart/mixed; boundary=BbC04y
--BbC04y
Content-Disposition: file; filename="essayfile.txt" 
Content-Type: text/plain
...contents of essayfile.txt...
--BbC04y
Content-Disposition: file; filename="imagefile.gif" 
Content-Type: image/gif
Content-Transfer-Encoding: binary
...contents of imagefile.gif...
--BbC04y--
--AaB03x--

3. 多部分范围响应

  • HTTP 对范围请求的响应也可以是多部分的。这样的响应中有 Content-Type: multipart/byteranges 首部和带有不同范围的多部分主体。
  • 下面是一个例子,展 了对文档不同范围的请求产生的响应:
HTTP/1.0 206 Partial content
Server: Microsoft-IIS/5.0
Date: Sun, 10 Dec 2000 19:11:20 GMT
Content-Location: http://www.joes-hardware.com/gettysburg.txt 
Content-Type: multipart/x-byteranges; boundary=--[abcdefghijklmnopqrstu vwxyz]--
Last-Modified: Sat, 09 Dec 2000 00:38:47 GMT

--[abcdefghijklmnopqrstuvwxyz]--
Content-Type: text/plain
Content-Range: bytes 0-174/1441
Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal. --[abcdefghijklmnopqrstuvwxyz]--
Content-Type: text/plain 
Content-Range: bytes 552-761/1441

But in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead who struggled here have consecrated it far above our poor power to add or detract.
--[abcdefghijklmnopqrstuvwxyz]-- 
Content-Type: text/plain 
Content-Range: bytes 1344-1441/1441

and that government of the people, by the people, for the people shall not perish from the earth.

--[abcdefghijklmnopqrstuvwxyz]--
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值