簡體   English   中英

HTTP / 1.1-傳輸編碼:分塊-響應中間的延遲

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

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

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

我需要知道問題出在哪里。 我應該在每個塊之后向服務器發送特殊消息嗎? 由於這種行為,我無法利用“保持活動”會話。

在幾次修改固件后都沒有成功之后,我回到代碼中嘗試不同的方法。 問題似乎是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')。 因此,這些額外的(意外)字符給我帶來了很多麻煩。 希望這也能解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM