HTTP/1.1 - 传输编码:分块 - 响应中间的延迟



我正在使用WINC1500 WiFi(带有Arduino(连接到服务器(https(并发送一些API请求。 在我发送的标题中:

POST /api/myapi.php HTTP/1.1
Host: myserver.com
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/x-www-form-urlencoded; charset=iso-8859-1
Connection: keep-alive
Content-Length: 10
my_content

服务器响应正常,如下所示:

HTTP/1.1 200 OK
Date: Tue, 03 Sep 2019 16:31:14 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json
18f
BlaBlaBla.... (399 chars = 18f in hexa)
0

问题是,在第一个块之后,等待终止块有延迟。最后一个块是空的。这是一个理解它的示例:

HTTP/1.1 200 OK 
Content-Type: text/plain 
Transfer-Encoding: chunked
7rn
Mozillarn 
9rn
Developerrn
7rn
Networkrn
0rn 
rn

还行。。。让我们看一下时间线:

19:31:13.685 -> connecting...
19:31:14.325 -> posting...
19:31:15.745 -> end posting...
19:31:15.864 ->     HTTP/1.1 200 OK
19:31:15.864 ->     Date: Tue, 03 Sep 2019 16:31:14 GMT
19:31:15.864 ->     Server: Apache
19:31:15.864 ->     Expires: Thu, 19 Nov 1981 08:52:00 GMT
19:31:15.864 ->     Cache-Control: no-store, no-cache, must-revalidate
19:31:15.864 ->     Pragma: no-cache
19:31:15.904 ->     Keep-Alive: timeout=5, max=100
19:31:15.904 ->     Connection: Keep-Alive
19:31:15.904 ->     Transfer-Encoding: chunked
19:31:15.904 ->     Content-Type: application/json
19:31:15.904 ->     
19:31:15.904 -> waitingChunkLength: 18f
19:31:15.904 -> 399
19:31:15.945 -> waitingChunk: BlaBlaBla.... (399 chars)
19:31:20.875 -> waitingChunkLength: 0
19:31:20.875 -> 0
19:31:20.875 -> waitingChunkTrailer: 
19:31:20.875 -> Done...
Connection closed

在收到第一个也是唯一的块后,在接收最后一个(终止(块之前会有一个巨大的延迟。实际上,最后一个块是在服务器关闭连接之前发送的。我相信这种情况的发生是因为空闲时间。有 5 秒不活动,因此服务器必须关闭连接。

我尝试在收到第一个块后向服务器发送消息("确定"(。在这种情况下,服务器会立即响应,但正在关闭连接(我相信是因为消息不足(:

19:45:43.818 -> connecting...
19:45:44.688 -> posting...
19:45:46.207 -> end posting...
19:45:46.322 ->     HTTP/1.1 200 OK
19:45:46.322 ->     Date: Tue, 03 Sep 2019 16:45:44 GMT
19:45:46.322 ->     Server: Apache
19:45:46.322 ->     Expires: Thu, 19 Nov 1981 08:52:00 GMT
19:45:46.322 ->     Cache-Control: no-store, no-cache, must-revalidate
19:45:46.322 ->     Pragma: no-cache
19:45:46.322 ->     Keep-Alive: timeout=5, max=100
19:45:46.358 ->     Connection: Keep-Alive
19:45:46.358 ->     Transfer-Encoding: chunked
19:45:46.358 ->     Content-Type: application/json
19:45:46.358 ->     
19:45:46.358 -> 
19:45:46.358 -> waitingChunkLength: 18f
19:45:46.358 -> 399
19:45:46.398 -> waitingChunk: BlaBlaBla.... (399 chars)
19:45:46.517 -> waitingChunkLength: 0
19:45:46.517 -> 0
19:45:46.517 -> waitingChunkTrailer: 
19:45:46.517 -> Done...
Connection closed

我需要知道问题出在哪里。我应该在每个块之后向服务器发送一条特殊消息吗?由于这种行为,我无法利用"保持活动"会话。

在更改固件几次但没有成功后,我回到代码并尝试不同的事情。 问题似乎是开机自检操作的最后一行:

client.print("Content-Length: ");
client.println(10);
client.println();
client.println("my_content");

最后一行应该是:

client.print("my_content");

println在文本后附加回车符(ASCII 13 或"\r"(和换行符(ASCII 10 或""(。所以,这些额外的(意想不到的(角色在那些日子里给我带来了很多麻烦。希望这也解决了您的问题。

最新更新