1、概述
超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议
所有的WWW文件都必须遵守这个标准
1.1 特点:
- 请求和响应必须成对出现
- 必须先有请求后有响应
- HTTP协议默认的端口:80
2、HTTP请求
客户端发送给服务器的信息
- GET请求:
GET /WEB09/demo1/subSucc.html?username=aaa&password=123 HTTP/1.1 Accept: text/html, application/xhtml+xml, */* X-HttpWatch-RID: 63397-10023 Referer: http://localhost:8080/WEB09/demo1/demo1.html Accept-Language: zh-Hans-CN,zh-Hans;q=0.5 User-Agent: Mozilla/5.0 (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 /WEB09/demo1/subSucc.html HTTP/1.1 Accept: text/html, application/xhtml+xml, */* X-HttpWatch-RID: 63397-10049 Referer: http://localhost:8080/WEB09/demo1/demo1.html Accept-Language: zh-Hans-CN,zh-Hans;q=0.5 User-Agent: Mozilla/5.0 (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: 25 Connection: Keep-Alive Cache-Control: no-cache username=aaa&password=123
2.1 组成部分
请求行 请求头 请求体
2.2 请求行
2.2.1 位置
请求信息的第一行
2.2.2 格式
请求方式 访问的资源 协议/版本
例如:GET /day0801/1.html HTTP/1.1
2.2.3 请求方式
get和post的不同点
- get会把参数放在url的后面 post不会
- get参数大小有限制(1KB),post请求却没有限制
- get请求没有请求体;post请求有请求体 请求参数放在请求体中
2.3 请求头
2.3.1 位置
请求信息的第二行到空行结束
2.3.2 格式
key/value (value可以是多个值)
2.3.3 常见的请求头
- Accept:支持数据类型 text/html text/css text/javascript 大类型/小类型 mime类型
- Accept-Charset:字符集
- Accept-Encoding:支持压缩
- Accept-Language:语言环境
- Host:访问主机
- If-Modified-Since:缓存文件的最后修改时间
- Referer:来自哪个页面、防盗链
- User-Agent:浏览器的内核
- Cookie
- Connection:Keep-Alive:链接状态
2.3.4 需要掌握的请求头
- Referer:来自哪个页面、防盗链
- User-Agent:浏览器的内核
- Cookie
- If-Modified-Since:缓存文件的最后修改时间
2.4 请求体
2.4.1 位置
空行以下的内容
只有post才有请求体
get请求参数 http://xxxx?username=tom&password=123
2.4.2 格式
username=tom&password=123
3、HTTP响应
服务器发送给浏览器的信息
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"147-1455670867735"
Last-Modified: Wed, 17 Feb 2016 01:01:07 GMT
Content-Type: text/html
Content-Length: 147
Date: Wed, 17 Feb 2016 01:17:06 GMT
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>HTTP响应</h1>
</body>
</html>
3.1 组成部分
响应行 响应头 响应体
3.2 响应行
3.2.1 位置
响应信息的第一行
3.2.2 格式
协议/版本 状态码 状态码说明
例如:HTTP/1.1 200 OK
3.2.3 状态码
- 200 正常响应成功
- 302 重定向
- 304 读缓存
- 404 用户操作资源不存在
- 500 服务器内部异常
3.3 响应头
3.3.1 位置
从响应信息的第二行到空行结束
3.3.2 常见的响应头
- Location:跳转方向,和302一起使用
- Server:服务器型号
- Content-Encoding:数据压缩
- Content-Length:数据长度
- Content-Language:语言环境
- Content-Type:数据类型
- Last-Modified:最后修改时间
- Refresh:定时刷新
- Content-Disposition:文件下载的时候使用的头信息
- Set-Cookie:
- Expires:缓存
- Cache-Control:缓存
- Pragma:缓存
- Connection:连接
- Date:日期
3.3.3 需要掌握的头信息
- Location:跳转方向,和302一起使用
- Content-Type:数据类型
- Last-Modified:最后修改时间
- Refresh:定时刷新
- Content-Disposition:文件下载的时候使用的头信息
- Set-Cookie:
3.4 响应体
3.4.1 位置
空行以后的内容
页面上展示的内容