LUA curl 请求同时设置超时时间

我使用LUA代码向后端发送json数据:

local cURL = require("cURL")
local c, err = cURL.easy{
  url = "http://10.10.10.10",
  post = true,
  httpheader = { "Content-Type: application/json"; },
  postfields = jsonString
}

local ok, err = c:perform()

除了一个问题以外,一切都很完美。如果我没有从服务器接收到响应,我的脚本将继续工作。我需要添加一些超时时间,如果在超时期间我没有收到响应,则关闭连接。

点赞
用户7253993
用户7253993

根据官方 文档easy 创建一个 Easy 对象,作为参数接收一个选项表:

c = curl.easy{
  url = 'http://example.com',
  [curl.OPT_VERBOSE] = true,
}

现在我认为您可以通过传递 CURLOPT_TIMEOUT 参数,以设置请求所允许的最长时间。所以在代码中:

local c, err = cURL.easy{
  url = "http://10.10.10.10",
  post = true,
  httpheader = { "Content-Type: application/json"; },
  postfields = jsonString,
  [curl.OPT_TIMEOUT] = 60, --您选择的超时时间
}

同样,我之前没有真正使用过这个参数,但我相信这与其他 CURLOPT 参数以相同方式运行。

2018-06-25 11:46:48