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

时间:2019-09-03 19:05:48

标签: chunked-encoding transfer-encoding

我正在使用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

问题在于,在第一个块之后,等待终止块会有延迟。最后一块是空的。这是example才能理解的:

HTTP/1.1 200 OK 
Content-Type: text/plain 
Transfer-Encoding: chunked

7\r\n
Mozilla\r\n 
9\r\n
Developer\r\n
7\r\n
Network\r\n
0\r\n 
\r\n

好吧...让我们看一下时间线:

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

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

1 个答案:

答案 0 :(得分:0)

在几次修改固件后都没有成功,我回到代码上尝试不同的方法。 问题似乎是POST操作的最后一行:

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

最后一行应该是:

client.print("my_content");

... println在文本后附加回车符(ASCII 13,或'\ r')和换行符(ASCII 10,或'\ n')。因此,这些额外的(意外)字符给我带来了很多麻烦。希望这也能解决您的问题。