---
title: "request和response中的content-type"
author: "dragonhunter"
date: "2016-12-21T14:39:52Z"
tags: []
categories: ["Web前端"]
slug: "request-response-content-type"
draft: false
lastmod: "2016-12-21T14:47:51Z"
id: 2346503
url: "https://dragonhunter.iteye.com/blog/2346503"
---

我们会经常看到http中的content-type这个属性。

content-type 这个属性在http的请求头和响应头都有这个属性。

一般我们用chrome访问一个url，比如http://www.baidu.com

可以看到Response headers有下面的属性，注意request headers没有content-Type

1. Connection:
   
   keep-alive
2. Content-Encoding:
   
   gzip
3. Content-Type:
   
   text/html; charset=utf-8
4. 这里的Content-Type是指返回的结果是text/html，浏览器根据这个属性可以正确展示内容。
5. 如果访问一个图片比如//www.baidu.com/img/bd\_logo1.png
6. Content-Type:
   
   image/png,可以看下response，是一些看不懂的数据，其实网络传输都是2进制文件，
7. 浏览器先把2进制文件解码为字符文件，如果content-type是text/html，浏览就会按照html规范把response解析界面格式。如果是image/png，就把response解析为图片。
8. 一般你百度到的content-type大多都是指response headers的的content-type，这个是不全面的。

再看下request headers的content-type,这个格式一般有multipart/form-data，application/x-www-form-urlencoded，application/json,

其中application/x-www-form-urlencoded是form表单提交的默认值，get和post请求，浏览器对请求数据参数处理会不一样的，multipart/form-data主要用于文件的上传。而application/json现在用的比较多，参数直接以json格式传递给服务器，在java代码中，服务器通过request.getInputStream来获取，并且application/json也可以用于response headers，告诉客户端，这是一个json格式的数据，restful api大多都是这种格式。