如何使用Lua获取HTTP响应体?

我需要解析通过HTTP GET请求获取的文本。 我在NodeMCU上使用Lua,但我不是非常熟悉它。

我正在使用脚本来获取响应并使用此脚本按行拆分它

local nStart, nEnd = string.find(c, "\n\n")
if (nEnde == nil) then
    nStart, nEnd = string.find(c, "\r\n\r\n")
end
c = string.sub(c,nEnd+1)
print("length: "..string.len(c))
data = mysplit(c, "\n") -- 填充文件名字段

HTTP GET请求看起来像这样

GET /lua/node.php?id=4022029&list HTTP/1.1
Host: mydomain.com
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)

当我打印HTTP响应时,它看起来像这样

HTTP/1.1 200 OK
Date: Tue, 28 Nov 2017 01:05:12 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=23p8rtds43pd1662ncm5cjhrl3; path=/
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

当我键入mydomain.com/lua/node.php?id=4022029&list时,我会得到一个文件的列表,但这个脚本没有获取任何东西。 看起来没有主体。 我错过了什么?

更新,

当从不编码数据块的HTTP中提取数据时,我的脚本可以工作,但我无法从数据块编码中提取数据。

点赞
用户1442917
用户1442917

Transfer-Encoding: chunked

你正在连接的服务器使用了 分块编码,这意味着头部应该跟着一个或多个包含内容长度和实际内容的块。看起来你要么还没有完全读取内容,要么你使用的库因某些原因无法处理分块内容。

2017-11-28 01:36:55