什么是http:
Hyper Text Transfer Protocol 超文本传输协议
* 传输协议:定义了,客户端和服务器端通信时,发送数据的格式
* 特点:
1. 基于TCP/IP的高级协议
2. 默认端口号:80
3. 基于请求/响应模型的:一次请求对应一次响应
4. 无状态的:每次请求之间相互独立,不能交互数据
1.Request:
请求消息字符串格式:
GET /zhang/testServlet HTTP/1.1
Host: localhost
Connection: keep-alive -->(支持链接复用)
Cache-Control: max-age=0 -->(缓存)
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36 -->浏览器版本信息,可以在服务器端获取该头的信息,解决浏览器的兼容性问题
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
origin:http://localhost-->本次请求,是从那个网站发起的
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Sec-Fetch-Site: none
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Referer: http://localhost/test.html---->告诉服务器,我(当前请求)从哪里来,1. 防盗链(实际上做不到,因为请求头可以伪装,一般用加权限,即登录,或者收费): 2. 统计工作:
Cookie: JSESSIONID=F3107A97E2CB0E72BF8384D68B564B8A
Content-Type:application/x-www-form-urlencoded; charset=UTF-8 -->;post的默认提交格式,将input的name、value用‘=’连接,不同的input之间用‘&’连接,x-www..可以改成json等。
-----请求空行-----
username=%E5%BC%A0%E5%AE%87%E6%B4%AA&password=123456&checkCode= (只有post请求才有)
2.Response功能:
* 响应字符串格式
HTTP/1.1 200 OK (设置状态码:setStatus(int sc) )
Content-Type: text/html;charset=UTF-8 setHeader(String name, String value)
Content-Length: 101
set-cookie:.....
Content-Disposition:attachment;filename="..." (attachmen:附属形式打开,也就是下载)
Date: Wed, 06 Jun 2018 07:08:42 GMT
------------空行---------------------------------------------------------------
<html> * 字符输出流:PrintWriter getWriter()
<head> * 字节输出流:ServletOutputStream getOutputStream()
<title>$Title$</title> *使用输出流,将数据输出到客户端浏览器
</head>
<body>
hello , response,经常这里是一个html页面
</body>
</html>