request和response中的content-type
我们会经常看到http中的content-type这个属性。
content-type 这个属性在http的请求头和响应头都有这个属性。
一般我们用chrome访问一个url,比如http://www.baidu.com
可以看到Response headers有下面的属性,注意request headers没有content-Type
-
Connection:
keep-alive
-
Content-Encoding:
gzip
-
Content-Type:
text/html; charset=utf-8
-
这里的Content-Type是指返回的结果是text/html,浏览器根据这个属性可以正确展示内容。
-
如果访问一个图片比如//www.baidu.com/img/bd_logo1.png
-
Content-Type:
image/png,可以看下response,是一些看不懂的数据,其实网络传输都是2进制文件,
-
浏览器先把2进制文件解码为字符文件,如果content-type是text/html,浏览就会按照html规范把response解析界面格式。如果是image/png,就把response解析为图片。
-
一般你百度到的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大多都是这种格式。