简体   繁体   English

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

[英]HTTP/1.1 - Transfer-Encoding: chunked - delay in the middle of response

I am using WINC1500 WiFi (with Arduino) to connect to a server (https) and to send some API requests. 我正在使用WINC1500 WiFi(与Arduino一起)连接到服务器(https)并发送一些API请求。 In the header I send: 在标题中,我发送:

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

The server response is ok, something like this: 服务器响应正常,如下所示:

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

The problem is that, after the first chunk, there is a delay in waiting for the terminating chunk. 问题在于,在第一个块之后,等待终止块会有延迟。 THe last chunk is an empty one. 最后一块是空的。 This is an example to understand it: 这是一个了解它的示例

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

Ok... Let's look at the time line: 好吧...让我们看一下时间线:

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

After receiving the first and only chunk, there is a huge delay before receiving the last (terminating) chunk. 在收到第一个也是唯一的块之后,在接收最后一个(终止)块之前会有很大的延迟。 Actually, the last chunk is send just before the connection to be closed by server. 实际上,最后一块在服务器关闭连接之前发送。 I believe that is happening because of the idle time. 我相信这是由于空闲时间而发生的。 There are 5 seconds of inactivity, so the server must close the connection. 有5秒钟的不活动时间,因此服务器必须关闭连接。

I try to send a message ("OK") to server just after receiving the first chunk. 我尝试在收到第一个块后立即向服务器发送消息(“确定”)。 In this case, the server is responding immediately, but is closing the connection (I believe because of inadequate message): 在这种情况下,服务器会立即做出响应,但是正在关闭连接(我相信是由于消息不足):

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

I need to know where is the problem. 我需要知道问题出在哪里。 Should I send a special message to server after each chunk? 我应该在每个块之后向服务器发送特殊消息吗? Because of this behaviour I cannot take advantage of "Keep-alive" session. 由于这种行为,我无法利用“保持活动”会话。

After changing firmware a couple of times without success, I go back to the code and try different things. 在几次修改固件后都没有成功之后,我回到代码中尝试不同的方法。 The problem seems to be last line in POST action: 问题似乎是POST操作的最后一行:

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

This last line should be: 最后一行应该是:

client.print("my_content");

... println appends a carriage return character (ASCII 13, or '\\r') and a newline character (ASCII 10, or '\\n') after text. ... println在文本后附加回车符(ASCII 13或'\\ r')和换行符(ASCII 10或'\\ n')。 So, this extra (unexpected) characters give me so much troubles those days. 因此,这些额外的(意外)字符给我带来了很多麻烦。 Hope this solve your problems too. 希望这也能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM