Http中Content-Type的取值讲解
在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息(MediaType,即是Internet Media Type,互联网媒体类型;也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息),但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值:
- 常见的媒体格式类型:Content-Type: text/html;charset:utf-8;
text/html : HTML格式
text/plain :纯文本格式
text/xml : XML格式
image/gif :gif图片格式
image/jpeg :jpg图片格式
image/png:png图片格式
2. 以application开头的媒体格式类型:
application/json : JSON数据格式
application/pdf :pdf格式
application/msword : Word文档格式
application/octet-stream : 二进制流数据(如常见的文件下载)
application/x-www-form-urlencoded 应用于表单
application/xml : XML数据格式
application/atom+xml :Atom XML聚合格式
3. 当在表单中需要上传文件的时候,使用的媒体格式类型是:multipart/form-data
二、四种常见的POST提交数据方式的媒体格式类型详解
1、application/json
application/json 这个 Content-Type 作为响应头大家肯定不陌生。实际上,现在越来越多的人把它作为请求头,用来告诉服务端消息主体是序列化后的 JSON 字符串。由于 JSON 规范的流行,除了低版本 IE 之外的各大浏览器都原生支持 JSON.stringify,服务端语言也都有处理 JSON 的函数,使用 JSON 不会遇上什么麻烦。
2、application/x-www-form-urlencoded(Content-Type: application/x-www-form-urlencoded;charset=utf-8 )
这应该是最常见的 POST 提交数据的方式了。浏览器的原生 form 表单,如果不设置 encType 属性,那么最终就会以 application/x-www-form-urlencoded 方式提交数据;
3、multipart/form-data
当使用表单上传文件时,必须设置encType属性为这个请求头
4、text/xml
尚未使用,如遇到的话会持续进行更新
1.模拟HTTP/get传递参数
1 2 3 4 5 6 7 8 9 10 | //PHP模拟get传参数 $url = "www.first.session.com" ; $hd = fsockopen ( $url ,80, $errno , $msg ,30); $header = "GET /file/demo01.php?name=lisi&age=20 HTTP/1.1" . "\r\n" ; $header .= "Host:$url" . "\r\n" ; $header .= "Content-type:application/x-www-form-urlencoded" . "\r\n" ; $header .= "\r\n" ; fwrite( $hd , $header ); $content = stream_get_contents( $hd ); var_dump( $content ); |
2.模拟HTTP/post请求传递参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //PHP模拟POST传参数 $url = "www.first.session.com" ; $fd = fsockopen ( $url ,80, $errno , $msg ,30); #-------------------------------------------------- $content = "----ABC\r\n" ; $content .= 'Content-Disposition:form-data;name="username"' . "\r\n\r\n" ; $content .= "lisi\r\n" ; #-------------------------------------------------- $content .= "----ABC\r\n" ; $content .= 'Content-Disposition:form-data;name="age"' . "\r\n\r\n" ; $content .= "20\r\n" ; #------------------------------------------------- $content .= "----ABC--\r\n\r\n" ; #------------------------------------------------- $out = "POST /file/demo01.php HTTP/1.1" . "\r\n" ; $out .= "HOST:$url\r\n" ; $out .= "Connection: keep-alive\r\n" ; $out .= "Content-Type:multipart/form-data;boundary=--ABC\r\n" ; $out .= "Content-Length:" . strlen ( $content ). "\r\n" ; $out .= "\r\n" ; $out .= $content ; fwrite( $fd , $out ); $contents = stream_get_contents( $fd ); var_dump( $contents ); |
3.php模拟HTTP文件上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //PHP模拟POST&文件上传 $url = "www.first.session.com" ; $fd = fsockopen ( $url ,80, $errno , $msg ,30); $content = "----ABC\r\n" ; $content .= 'Content-Disposition:form-data;name="username"' . "\r\n\r\n" ; $content .= "lisi\r\n" ; #------------------------------------------------------------- $content .= "----ABC\r\n" ; $content .= 'Content-Disposition:form-data;name="ufile";filename="timg.jpg"' . "\r\n" ; $content .= "Content-Type:image/jpg\r\n\r\n" ; $content .= file_get_contents ( "./timg.jpg" ). "\r\n" ; $content .= "----ABC--" . "\r\n\r\n" ; #------------------------------------------------------------- $out = "POST /file/demo01.php HTTP/1.1" . "\r\n" ; $out .= "HOST:$url\r\n" ; $out .= "Connection: keep-alive\r\n" ; $out .= "Content-Type:multipart/form-data;boundary=--ABC\r\n" ; $out .= "Content-Length:" . strlen ( $content ). "\r\n" ; $out .= "\r\n" ; $out .= $content ; fwrite( $fd , $out ); $contents = stream_get_contents( $fd ); var_dump( $contents ); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!