Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用lua发送Get到网站_Lua_Luasocket - Fatal编程技术网

用lua发送Get到网站

用lua发送Get到网站,lua,luasocket,Lua,Luasocket,我和lua有麻烦了 我需要发送一个请求到一个网站的获取和得到的响应从网站 我只有这个: local LuaSocket = require("socket") client = LuaSocket.connect("example.com", 80) client:send("GET /login.php?login=admin&pass=admin HTTP/1.0\r\n\r\n") while true do s, status, partial = client:receiv

我和lua有麻烦了

我需要发送一个请求到一个网站的获取和得到的响应从网站

我只有这个:

local LuaSocket = require("socket")
client = LuaSocket.connect("example.com", 80)
client:send("GET /login.php?login=admin&pass=admin HTTP/1.0\r\n\r\n")
while true do
  s, status, partial = client:receive('*a')
  print(s or partial)
  if status == "closed" then 
    break 
  end
end
client:close()
我应该如何从服务器获取响应

我想发送一些信息到这个网站,并获得网页的结果


有什么想法吗?

这可能不起作用,因为在连接关闭之前,将一直在读取,但在这种情况下,客户端不知道要读取多少。您需要做的是逐行读取并解析标题以查找内容长度,然后在看到两个行尾后读取指定数量的字节(如Content Length中设置的)

socket.http将为您解决所有这些复杂问题,而不是自己完成(读取和解析标题、处理重定向、100 continue等等)。试着这样做:

local http = require("socket.http")
local body, code, headers, status = http.request("https://www.google.com")
print(code, status, #body)

我解决了通过标题的问题

local LuaSocket = require("socket")
client = LuaSocket.connect("example.com", 80)
client:send("GET /login.php?login=admin&pass=admin HTTP/1.0\r\nHost: example.com\r\n\r\n")
while true do
  s, status, partial = client:receive('*a')
  print(s or partial)
  if status == "closed" then 
    break 
  end
end
client:close()
打印(或部分打印)
返回什么?如果它正在打印nil,那么您的连接未建立。否则,
s
包含来自服务器的响应(包括标题信息)。