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

  • 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]--
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Content-TypeMIME 类型(Multipurpose Internet Mail Extensions)是HTTP协议中用于指示发送到服务器的数据类型的重要头信息。它们对于正确处理网页、图片、文档等不同类型的资源至关重要。 设置 `Content-Type` 的步骤通常是: 1. 在HTTP请求或响应的头部添加一个 `Content-Type` 字段。 2. 值通常包括两个部分:类型名和/或子类型名,用斜杠分隔,例如 "text/html" 或 "image/jpeg"。 - 类型名如 "text" 表示数据是文本性质,"image" 表示图像,"application" 表示应用数据。 - 子类型名提供更具体的分类,比如 "html" 对应 HTML 文档,"jpeg" 对应 JPEG 图像格式。 常见的 Content-Type 和对应的 MIME 类型包括: - text/plain: 纯文本 - text/html: HTML 文档 - application/json: JSON 数据 - image/jpeg, image/png: 图像文件 - application/pdf: PDF 文件 在编程中,特别是使用HTTP客户端库时,你可能需要根据具体操作自动设置这些值,比如在发送POST请求时指定表单数据的格式,或者在响应构建器中设置返回内容的类型。这里是一些常见编程语言的例子: ```python (requests库): import requests response = requests.post('https://example.com', data={'key': 'value'}, headers={'Content-Type': 'application/x-www-form-urlencoded'}) java (HttpClient API): CloseableHttpResponse response = httpClient.execute(HttpPost("http://example.com"), new BasicHttpEntity<>(new StringEntity(jsonData), ContentType.APPLICATION_JSON)); JavaScript (fetch API): fetch(url, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值