http请求参数Accept与Content-Type区分

Accept属于请求头,描述客户端希望接收的响应body 数据类型。
Content-Type属于实体头,描述客户端发送的body 数据类型。
例如:
Accept:text/xml; 
Content-Type:text/html 
希望接收的数据类型是xml格式,本次请求发送的数据的数据格式是html。
常见的媒体格式类型如下:
    text/html : HTML格式
    text/plain :纯文本格式      
    text/xml :  XML格式
    image/gif :gif图片格式    
    image/jpeg :jpg图片格式 
    image/png:png图片格式
以application开头的媒体格式类型:
   application/xhtml+xml :XHTML格式
   application/xml     : XML数据格式
   application/atom+xml  :Atom XML聚合格式    
   application/json    : JSON数据格式
   application/pdf       :pdf格式  
   application/msword  : Word文档格式
   application/octet-stream : 二进制流数据(如常见的文件下载)
   application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)
另外一种常见的媒体格式是上传文件之时使用的:
    multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式。
常见问题答疑
1、*/*的作用?
"Accept",   "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*",一大段资源,在最后又加上了*/*,这是为什么?
客户端支持这些类型,并指定了希望得到类型的优先级,如果没有,依次表达意愿 !
2、服务端侧怎么实现的?
SpringMvc为例,提供了多种MessageConverter,用户也可以自己扩展实现
框架启动的时候,会装载能支持的各种MessageConverter
请求来的时候,根据上述设置依次查找本地是否有对应的MessageConverter,如果找到就用找到的MessageConverter返回对应的类型数据。

转载于:https://my.oschina.net/artong999/blog/3058556

猜你喜欢

转载自blog.csdn.net/weixin_33782386/article/details/92042690