为什么网络服务器在发送代码 200(确定)后响应代码 400(错误请求)
Why is a webserver responding with code 400 (BAD REQUEST) AFTER it has sent a code 200 (OK)
对于一个项目,我正在尝试在客户端和网络服务器之间建立 TCP 连接。当我向网络服务器的根目录发出获取请求时,我首先收到 200(OK) 响应,但之后网络服务器发送 400(Bad request) 响应。请注意,我没有发送任何额外的请求,因此这是对我的第一个请求的第二个响应。我的网络服务器 运行 是一个简单的 node.js 服务器 returns “Hello World”。
向网络服务器请求
GET / HTTP/1.1
Host: localhost:8001
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7
来自网络服务器的响应
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Tue, 18 Aug 2020 09:13:06 GMT
Connection: keep-alive
Transfer-Encoding: chunked
c
Hello world
0
HTTP/1.1 400 Bad Request
Connection: close
网络服务器代码
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello world\n')
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');
连接的 Wireshark 打印输出
我找到了我的问题的答案,它非常简单,我花了两天时间才找到它。我用来通过 tcp 连接发送到网络服务器的缓冲区是 4096 字节长,但我的消息要短得多。空字节通过单独的请求发送并导致 400 Bad Request 响应。我现在 trim 我的缓冲区在发送任何请求之前消除其中的所有空 space。感谢所有帮助我的人!!
对于一个项目,我正在尝试在客户端和网络服务器之间建立 TCP 连接。当我向网络服务器的根目录发出获取请求时,我首先收到 200(OK) 响应,但之后网络服务器发送 400(Bad request) 响应。请注意,我没有发送任何额外的请求,因此这是对我的第一个请求的第二个响应。我的网络服务器 运行 是一个简单的 node.js 服务器 returns “Hello World”。
向网络服务器请求
GET / HTTP/1.1
Host: localhost:8001
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7
来自网络服务器的响应
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Tue, 18 Aug 2020 09:13:06 GMT
Connection: keep-alive
Transfer-Encoding: chunked
c
Hello world
0
HTTP/1.1 400 Bad Request
Connection: close
网络服务器代码
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello world\n')
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');
连接的 Wireshark 打印输出
我找到了我的问题的答案,它非常简单,我花了两天时间才找到它。我用来通过 tcp 连接发送到网络服务器的缓冲区是 4096 字节长,但我的消息要短得多。空字节通过单独的请求发送并导致 400 Bad Request 响应。我现在 trim 我的缓冲区在发送任何请求之前消除其中的所有空 space。感谢所有帮助我的人!!