curl - 带有超时的 LUA curl 请求
问题描述
我使用 LUA 代码将 json-data 发送到后端:
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()
除了一件事,一切都完美无缺。如果我没有收到来自服务器的响应,我的脚本会继续工作。我需要添加一些超时,如果在超时期间我没有收到响应,请关闭连接。
解决方案
根据官方文档 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, --Your timeout of choice
}
同样,我之前不必实际使用此参数,但我相信它的工作方式与其他CURLOPT
参数相同。
推荐阅读
- c++ - 引用向量中的成员
不断弹出错误 - google-chrome - 我的电脑和手机上只加载了一个网站
- java - 将 Docker Sqlite 容器连接到 Java
- matlab - 在 MATLAB 中运行 PARFOR 循环时内存不足
- arrays - 如何在 jolt 中创建特定的数组
- c# - 根据字母的位置拆分字母
- flutter - 我如何在颤动中访问对象中的值
- javascript - 以中间件的形式表达 Try and Catch
- spring - 如何在spring boot中从所有jsp访问taglibs
- python - 基于 Python 中数据框的两列创建包含多个级别的新变量