HTTP请求与响应

2018-05-06  本文已影响0人  leiuno

HTTP

HTTP:Hyper Text Transfer Protocol(超文本传输协议)的缩写,HTTP是一个基于TCP/IP通信协议来传递数据,默认端口号为80。

通常,由HTTP客户端(Client)发起一个请求,建立一个到服务器(Server)指定端口(默认是80端口)的TCP连接。HTTP服务器则在那个端口监听客户端发送过来的请求。一旦收到请求,服务器(向客户端)发回一个状态行,比如"HTTP/1.1 200 OK",和(响应的)消息,消息的消息体可能是请求的文件、错误消息、或者其它一些信息。

总结:

HTTP请求

1、curl 创造一个请求

(1)get请求

curl -s -v -H "Frank: xxx" -- "https://www.baidu.com"

请求内容

> GET / HTTP/1.1
> Host: www.baidu.com  //告知访问的域名
> User-Agent: curl/7.54.0  //用的是什么软件发起的响应
> Accept: */*     //接受返回的任何内容
> leiuno: xxx
> 

(2)post请求

curl -X POST -s -v -H "leiuno: xxx" -- "https://www.baidu.com"

请求内容

> POST / HTTP/1.1  //post
> Host: www.baidu.com
> User-Agent: curl/7.54.0
> Accept: */*
> leiuno: xxx
> 

(3)post一段数据

curl -X POST -d "1234567890" -s -v -H "leiuno: xxx" -- "https://www.baidu.com"

请求内容

> POST / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.54.0
> Accept: */*
> Frank: xxx
> Content-Length: 10  //上传10个字节
> Content-Type: application/x-www-form-urlencoded //上传格式
> 
> 1234567890

2、请求格式总结

1 动词 路径 协议/版本
2 Key1: value1
2 Key2: value2
2 Key3: value3
2 Content-Type: application/x-www-form-urlencoded
2 Host: www.baidu.com
2 User-Agent: curl/7.54.0
3 
4 要上传的数据

(1)请求最多包含四部分,最少包含三部分。(也就是说第四部分可以为空)

(2)第三部分永远都是一个回车(\n)

(3)动词有 GET(获取内容) POST(上传内容,一般登陆的时候) PUT(整体更新) PATCH(局部更新) DELETE HEAD OPTIONS 等

(4)这里的路径包括「查询参数」,但不包括「锚点」

(5)如果你没有写路径,那么路径默认为 /

(6)第 2 部分中的 Content-Type 标注了第 4 部分的格式

3、用 Chrome 发请求
(1)打开 Network
(2)地址栏输入网址
(3)在 Network 点击,查看 request,点击「view source」
(4)如果有请求的第四部分,那么在 FormData 或 Payload 里面可以看到

HTTP响应

发生请求之后,会的到一个响应,除非断网了或者服务器宕机了

1、get请求的响应

< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2443 //下载内容的长度
< Content-Type: text/html //下载内容的类型
< Date: Sun, 06 May 2018 13:16:27 GMT
< Etag: "58860429-98b"
< Last-Modified: Mon, 23 Jan 2017 13:24:57 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18  //百度自行的标记
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< 
<!DOCTYPE html>

2、post请求的响应

< HTTP/1.1 302 Found
< Connection: Keep-Alive
< Content-Length: 17931
< Content-Type: text/html
< Date: Sun, 06 May 2018 13:28:16 GMT
< Etag: "54d9748e-460b"
< Server: bfe/1.0.8.18
< 
<html>

3、响应的格式

1 协议/版本号 状态码 状态解释
2 Key1: value1
2 Key2: value2
2 Content-Length: 17931
2 Content-Type: text/html
3
4 要下载的内容

4、用 Chrome 查看响应

(1)打开 Network

(2)输入网址

(3)选中第一个响应

(4)查看 Response Headers,点击「view source」

(5)查看 Response 或者 Preview,你会看到响应的第 4 部分

上一篇 下一篇

猜你喜欢

热点阅读