Lua套接字示例,尝试不同的网站。

我正在使用此示例。它基于此网站http://www.google.com/robots.txt输出文本

local socket = require("socket")
client = socket.connect("google.com", 80)
client:send("GET /robots.txt HTTP/1.0\r\n\r\n")
while true do
  s, status, partial = client:receive(1024)
  print(s or partial)
  if status == "closed" then
    break
  end
end
client:close()

我使用:

local socket = require("socket")
client = socket.connect("www.lua.org", 80)
client:send("GET /pil/9.4.html HTTP/1.0\r\n\r\n")
while true do
  s, status, partial = client:receive(1024)
  print(s or partial)
  if status == "closed" then
    break
  end
end
client:close()

但是,如上所述,我尝试了此链接http://www.lua.org/pil/9.4.html,但失败了,显示“HTTP/1.0 302 Moved temporarily”。在许多其他网站上都执行了同样的操作,结果相似。为什么会这样?非常感谢

点赞
用户1861412
用户1861412

你收到了一个重定向(HTTP 302 状态码)应该通过发出新的请求到指定的Location来处理。

你可以使用 socket 库的高级 HTTP 模块,而不是使用原始套接字,它提供了一个 request 方法,能够自动跟随重定向。

2014-10-18 17:34:33