Android开发Android开发经验谈Android开发

Http协议请求和响应报文字段详解

2019-08-21  本文已影响8人  手指乐

Http协议请求报文组成

HTTP请求报文由3部分组成(请求行+请求头+请求体):

下面是一个实际的请求报文:

请求头字段解析

以这个报文为例:

POST /app/game/game_list/ HTTP/1.1
Content-Length: 10
Content-Type: application/x-www-form-urlencoded
Host: zhushou.72g.com
Connection: Keep-Alive
Accept-Encoding: gzip


platform=2

1.Post:代表请求写协议,一般是get或post,区别:

  1. Content-Length:body中内容的长度
    3.Content-Type:body中内容的类型,默认为application/x-www-form-urlencoded,使用url编码的表单数据类型,还可以指定内容的编码:Content-Type: application/x-www-form-urlencoded;charset=utf-8
    其他常见类型:
POST http://www.example.com HTTP/1.1 
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA 

------WebKitFormBoundaryrGKCBY7qhFd3TrwA 
Content-Disposition: form-data; name="text" 

title 
------WebKitFormBoundaryrGKCBY7qhFd3TrwA 
Content-Disposition: form-data; name="file"; filename="chrome.png" 
Content-Type: image/png 

PNG ... content of chrome.png ... 
------WebKitFormBoundaryrGKCBY7qhFd3TrwA-- 

这个例子稍微复杂点。首先生成了一个 boundary 用于分割不同的字段,为了避免与正文内容重复,boundary 很长很复杂。然后 Content-Type 里指明了数据是以 mutipart/form-data 来编码,本次请求的 boundary 是什么内容。消息主体里按照字段个数又分为多个结构类似的部分,每部分都是以 --boundary 开始,紧接着内容描述信息,然后是回车,最后是字段具体内容(文本或二进制)。如果传输的是文件,还要包含文件名和文件类型信息。消息主体最后以 --boundary-- 标示结束。
这种方式一般用来上传文件,各大服务端语言对它也有着良好的支持。
上面提到的这两种 POST 数据的方式,都是浏览器原生支持的,而且现阶段原生 form 表单也只支持这两种方式。

POST http://www.example.com HTTP/1.1 
Content-Type: application/json;charset=utf-8 

{"title":"test","sub":[1,2,3]} 
POST http://www.example.com HTTP/1.1 
Content-Type: text/xml 

<!--?xml version="1.0"?--> 
<methodcall> 
    <methodname>examples.getStateName</methodname> 
    <params> 
        <param> 
            <value><i4>41</i4></value> 
         
    </params> 
</methodcall> 
  1. Host: zhushou.72g.com:服务器地址
  2. Connection: Keep-Alive :客户端和服务器保持长时间链接
  3. Accept-Encoding: gzip:接收的响应数据,编码格式应该为gzip

HTTP的响应报文结构

HTTP的响应报文也由三部分组成(响应行+响应头+响应体)
以下是一个实际的HTTP响应报文:


①报文协议及版本;
②状态码及状态描述;
③响应报文头,也是由多个属性组成;
④响应报文体,即我们真正要的“干货”。

响应报文字段解析:

  1. 响应状态码
    HTTP的响应状态码由5段组成:
  1. Content-Type:响应报文的类型
  2. Content-Encoding:响应报文的编码
上一篇 下一篇

猜你喜欢

热点阅读