发送到lua的网站

时间:2014-07-04 13:30:38

标签: lua luasocket

我在使用lua时遇到了麻烦。

我需要通过GET向网站发送请求并从网站获取回复。

Atm我拥有的就是:

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()

如何从服务器获取响应?

我想向本网站发送一些信息并获取页面结果。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

这可能不会起作用,因为*a将会一直阅读,直到连接关闭,但在这种情况下,客户端并不知道要阅读多少。您需要做的是逐行读取并解析标题以查找Content-Length,然后在看到两行结束后,您将读取指定的字节数(在Content-Length中设置)。

socket.http代替自己完成所有操作(阅读和解析标题,处理重定向,100继续,以及所有这些),local http = require("socket.http") local body, code, headers, status = http.request("https://www.google.com") print(code, status, #body) 将为您处理所有复杂性。尝试这样的事情:

{{1}}

答案 1 :(得分:0)

大家好我解决了传递标题的问题

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()