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

来源:互联网 发布:淘宝客佣金在哪设置 编辑:程序博客网 时间:2024/05/20 14:22
  • 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 --AaB03xContent-Disposition: form-data; name="submit-name" Sally--AaB03xContent-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 --AaB03xContent-Disposition: form-data; name="submit-name" Sally--AaB03xContent-Disposition: form-data; name="files" Content-Type: multipart/mixed; boundary=BbC04y--BbC04yContent-Disposition: file; filename="essayfile.txt" Content-Type: text/plain...contents of essayfile.txt...--BbC04yContent-Disposition: file; filename="imagefile.gif" Content-Type: image/gifContent-Transfer-Encoding: binary...contents of imagefile.gif...--BbC04y----AaB03x--

3. 多部分范围响应

  • HTTP 对范围请求的响应也可以是多部分的。这样的响应中有 Content-Type: multipart/byteranges 首部和带有不同范围的多部分主体。
  • 下面是一个例子,展 了对文档不同范围的请求产生的响应:
HTTP/1.0 206 Partial contentServer: Microsoft-IIS/5.0Date: Sun, 10 Dec 2000 19:11:20 GMTContent-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/plainContent-Range: bytes 0-174/1441Fourscore 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/1441But 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/1441and that government of the people, by the people, for the people shall not perish from the earth.--[abcdefghijklmnopqrstuvwxyz]--
原创粉丝点击