http协议
一. HTTP协议概述
- HTTP是HyperText Transfer Protocol(超文本传输协议)的简写,传输HTML文件。
- 作用: 用于定义WEB浏览器与WEB服务器之间交换数据的过程和数据本身的内容
- 浏览器和服务器交互过程: 浏览器请求, 服务请求响应
请求包括: 请求行, 请求头, 请求体.
响应: 响应行, 响应头, 响应体
- Google抓包
- Firefox
- IE抓包:
二. 请求部分
-get请求
GET /loginApp/success.html?username=zs&password=123456 HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
X-HttpWatch-RID: 89102-10094
Referer: http://localhost:8080/loginApp/login.html
Accept-Language: zh-Hans-CN,zh-Hans;q=0.5
User-Agent: Mozilla/5.0 (MSIE 9.0; qdesk 2.4.1266.203; Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost:8080
Connection: Keep-Alive
- post请求
POST /loginApp/success.html HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
X-HttpWatch-RID: 58116-10147
Referer: http://localhost:8080/loginApp/login.html
Accept-Language: zh-Hans-CN,zh-Hans;q=0.5
User-Agent: Mozilla/5.0 (MSIE 9.0; qdesk 2.4.1266.203; Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:8080
Content-Length: 27
Connection: Keep-Alive
Cache-Control: no-cache
username=zs&password=123456
- 请求行
GET /loginApp/success.html?username=zs&password=123456 HTTP/1.1
POST /loginApp/success.html HTTP/1.1
- 请求方式(7种,put,delete等)
GET:明文传输, 不安全,参数跟在请求路径后面,对请求参数大小有限制,
POST: 暗文传输,安全一些,请求参数在请求体里,对请求参数大小没有有限制, - URI:统一资源标识符(即:去掉协议和IP地址部分)
- 协议版本:HTTP/1.1
-
请求头
从第2行到空行处,都叫请求头,以键值对的形式存在,但存在一个key对应多个值的请求头.作用:浏览器告诉服务器相关的设置.
- Accept:浏览器可接受的MIME类型 ,告诉服务器客户端能接收什么样类型的文件。
- User-Agent:浏览器信息.
User-Agent: Mozilla/5.0 (MSIE 9.0; qdesk 2.4.1266.203; Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
- Accept-Charset: 浏览器通过这个头告诉服务器,它支持哪种字符集
- Content-Length:表示请求参数的长度
- Host:初始URL中的主机和端口
- Referrer:来自哪个页面、防盗链
Referer: http://localhost:8080/loginApp/login.html - Content-Type:内容类型,告诉服务器,浏览器传输数据的MIME类型,文件传输的类型,application/x-www-form-urlencoded .
Content-Type: application/x-www-form-urlencoded - Accept-Encoding:浏览器能够进行解码的数据编码方式,比如gzip
- Connection:表示是否需要持久连接。如果服务器看到这里的值为“Keep -Alive”,或者看到请求使用的是HTTP 1.1(HTTP 1.1默认进行持久连接 )
- Cookie:这是最重要的请求头信息之一(会话技术, 后面会有专门的时间来讲的)
- Date:Date: Mon, 22Aug 2011 01:55:39 GMT请求时间GMT
2.3请求体
- 只有请求方式是post的时候,才有请求体. 就是post方式请求参数所在的位置
三. 响应部分
- 响应部分
HTTP/1.1 200 ok
Accept-Ranges: bytes
ETag: W/"158-1539331624457"
Last-Modified: Thu., 19 Sep 2019 20:39:04 GMT
Content-Type: text/html
Content-Length: 158
Date: Thu., 19 Sep 2019 20:41:46 GMT
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Login Success</h1>
</body>
</html>
1. 响应行
HTTP/1.1 200 OK
- 协议/版本
- 响应状态码
- 200:正常,成功
302:重定向
304:表示客户机缓存的版本是最新的,客户机可以继续使用它,无需到服务器请求. 读取缓存
404:客户端错误(一般是路径写错了,没有这个资源)
500:服务器内部错误
2. 响应头
服务器指示浏览器去做什么, 去配什么
-
Location: http://www.it315.org/index.jsp指示新的资源的位置,通常和状态,码302一起使用,完成请求重定向
-
Content-Type: text/html; charset=UTF-8; 设置服务器发送的内容的MIME类型,文件下载时候
-
Refresh: 5;url=http://www.it315.org指示客户端刷新频率。单位是秒
-
Content-Disposition: attachment; filename=a.jpg 指示客户端(浏览器)下载文件
-
Content-Length:80 告诉浏览器正文的长度
-
Server:apachetomcat 服务器的类型
-
Content-Encoding: gzip服务器发送的数据采用的编码类型
-
Set-Cookie:SS=Q0=5Lb_nQ;path=/search服务器端发送的Cookie
-
Cache-Control: no-cache (1.1)
-
Pragma: no-cache (1.0) 表示告诉客户端不要使用缓存
-
Connection:close/Keep-Alive
-
Date:Tue, 11 Jul 2000 18:23:51 GMT
3. 响应体
- 就是浏览器所看到的内容.
页面展示内容, 和网页右键查看的源码一样