我们用测试工具例如postman测试post或者get返回内容是很正常的。但是从编程调试可以看到变量的内容是这样的:
postman:
HTTP/1.1 200 OK
Date: Fri, 09 Sep 2022 02:00:23 GMT
Server: Apache
Upgrade: h2,h2c
Connection: Upgrade
Transfer-Encoding: chunked
Content-Type: text/plain
length is 4the content length is 4POST DATA: AE
c++调试:
HTTP/1.1 200 OK
Date: Fri, 09 Sep 2022 02:32:51 GMT
Server: Apache
Upgrade: h2,h2c
Connection: Upgrade, Keep-Alive
Keep-Alive: timeout=500, max=100
Transfer-Encoding: chunked
Content-Type: text/plain
31
length is 4the content length is 4POST DATA: AE
之前没有弄懂这个31的意义,后面好不容易找到一段解答:
这个是http协议中的chunked分组,对于长度较长无法一下确定内容长度的网页,服务器就会将网页内容分块发送,然后在本地重新解码、组装。
基本格式是[Chunk大小][回车][Chunk数据体][回车][Chunk大小][回车][Chunk数据体][回车][0][回车]
根据这个格式,用c/c++解码是比较简单的,不过需要注意的是chunk大小是16进制的ascii形式,这个需要进行转换,详细的介绍baidu或google “http协议 chunked”
如上的31则是3*16+1=49个字节。数一数后面的内容果然是。