其可选值有:
也可以同时指定多个值,用逗号分隔,像这样: 其中, 在 Web Socket 没出来前,可利用这一机制实现长连接的效果。 示例以 Node.js 为例的 var http = require("http");
function generateChunk(index, response) {
setTimeout(() => {
if (index === 5) {
response.write("end");
response.end("</body></html>");
} else {
response.write(`<p> chunk ${index}</p>`);
}
}, index * 1000);
}
function handlerRequest(_request, response) {
response.setHeader("Content-Type", "text/html; charset=UTF-8");
response.setHeader("Transfer-Encoding", "chunked");
response.write(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTTP 分块传输示例</title>
</head>
<body>
<h1>HTTP 分块传输示例</h1>
`);
let index = 0;
while (index <= 5) {
generateChunk(index, response);
index++;
}
}
const server = http.createServer(handlerRequest);
server.listen(3000);
console.log("server started at http://localhost:3000");
Transfer-Encoding:chunked 分块传输示例 总结HTTP/2 中已经不支持 相关资源 |
转载于:https://www.cnblogs.com/Wayou/p/http_transfer_encoding_chunked.html