HTTP
1.协议---计算机世界的语言
协议是定义了 ** 数据 ** 在 **计算机内和之间**进行**交换**的**规则**的**系统**。
设备之间通信要求设备接受正在交换的数据的格式。**定义格式的一组规则称之为协议**。
2.HTTP协议 它是 Web 上进行任何数据交换的基础 是一种 客户端—服务器(client-server)协议
1. websocket 虽然不是http协议 但是他的链接建立需要先用http协议握手
WebSocket 连接的建立始于一个 HTTP 请求。客户端发送一个特殊的 HTTP 请求到服务器,该请求包含了一个 Upgrade 头信息,表明希望将当前连接升级为 WebSocket 协议。如果服务器同意,它会返回一个 HTTP 101 Switching Protocols 响应,表示协议切换成功,之后双方就可以通过 WebSocket 进行通信了。
2. Server Push 也是基于http协议的
1. Server-Sent Events (SSE) 是一种允许服务器向浏览器推送实时更新的技术。它提供了一种简单的方法来建立从服务器到客户端的单向通信通道。
3. http是无状态的,通过cookie实现了会话
1. 尽管 HTTP 根本上来说是无状态的,但借助 HTTP Cookie 就可使用有状态的会话。利用标头的扩展性,HTTP Cookie 被加进了协议工作流程,每个请求之间就能够创建会话,让每个请求都能共享相同的上下文信息或相同的状态。
2. JWT默认是无状态的,**因为服务端默认不保存token,每次解析token获取用户身份**
你可以选择让服务器端也保存一份令牌或与之关联的数据,从而创建一种“有状态”的行为。例如,服务器可以维护一个黑名单或白名单来跟踪已签发但已被撤销的令牌。此外,某些情况下,服务器可能会缓存用户的偏好设置或其他会话相关的数据,以便快速响应请求。
3.抓取HTTP包
;
const url = 'https://jsonplaceholder.typicode.com/posts/1';
const headers = {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8',
'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Content-Length': '',
'Content-Type': 'application/json',
'Cookie': 'sessionId=38afes7a8; csrftoken=a8dfc6be79jvhdsuyugdfeuh',
'Date': new Date().toUTCString(),
'Expect': '',
'From': 'user@example.com',
'Host': 'jsonplaceholder.typicode.com',
'If-Match': '"737060cd8c284d8af7ad3082f209582d"',
'If-Modified-Since': 'Sat, 29 Oct 1994 19:43:31 GMT',
'If-None-Match': '"737060cd8c284d8af7ad3082f209582d"',
'If-Unmodified-Since': 'Sat, 29 Oct 1994 19:43:31 GMT',
'Max-Forwards': '10',
'Origin': 'http://www.example-social-network.com',
'Pragma': 'no-cache',
'Proxy-Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
'Range': 'bytes=200-1000',
'Referer': 'https://example.com/index.html',
'TE': 'trailers, deflate',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
'Upgrade': 'websocket',
'Via': '1.0 proxy.example.com:8080',
'Warning': '199 Miscellaneous warning'
};
axios.get(url, { headers })
.then(response => {
console.log('Response Data:', response.data);
})
.catch(error => {
console.error('Error:', error.message);
});
```
响应示例
```js
const express = require('express');
const app = express();
const port = 3000;
app.get('/example', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.setHeader('Access-Control-Max-Age', '86400');
res.setHeader('Cache-Control', 'public, max-age=31536000');
res.setHeader('Connection', 'keep-alive');
res.setHeader('Content-Encoding', 'gzip');
res.setHeader('Content-Language', 'en-US');
res.setHeader('Content-Length', '');
res.setHeader('Content-Type', 'application/json');
res.setHeader('Date', new Date().toUTCString());
res.setHeader('ETag', '"737060cd8c284d8af7ad3082f209582d"');
res.setHeader('Expires', new Date(Date.now() + 31536000000).toUTCString());
res.setHeader('Last-Modified', 'Wed, 21 Oct 2015 07:28:00 GMT');
res.setHeader('Location', 'https://www.example.com/new-page');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Retry-After', '120');
res.setHeader('Server', 'CustomServer/1.0');
res.setHeader('Set-Cookie', 'sessionId=38afes7a8; Path=/; HttpOnly');
res.setHeader('Transfer-Encoding', 'chunked');
res.setHeader('Vary', 'Accept-Language');
res.setHeader('Via', '1.0 proxy.example.com:8080');
res.setHeader('Warning', '199 Miscellaneous warning');
const responseData = {
message: 'Hello, World!',
id: 1,
title: 'Example Post'
};
res.send(responseData);
});
app.listen(port, () => {
console.log(`Server is running on http:
});
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
2021-12-16 java se