我正在尝试通过https使用带有luasec的lua从Web服务器检索页面。对于大多数页面,我的脚本按预期工作,但如果资源包含特殊字符(例如'é),我将被发送到一个包含301个响应的循环中。
让这段代码片段说明我的困境(实际的服务器详细信息被编辑以保护无辜者):
local https = require "ssl.https"
local prefix = "https://www.example.com"
local suffix = "/S%C3%A9ance"
local body,code,headers,status = https.request(prefix .. suffix)
print(status .. " - GET was for \"" .. prefix .. suffix .. "\"")
print("headers are " .. myTostring(headers))
print("body is " .. myTostring(body))
if suffix == headers.location then
print("equal")
else
print("not equal")
end
local body,code,headers,status = https.request(prefix .. headers.location)
print(status .. " - GET was for \"" .. prefix .. suffix .. "\"")
导致矛盾
HTTP/1.1 301 Moved Permanently - GET was for "https://www.example.com/S%C3%A9ance"
headers are { ["content-type"]="text/html; charset=UTF-8";["set-cookie"]="PHPSESSID=e80oo5dkouh8gh0ruit7mj28t6; path=/";["content-length"]="0";["connection"]="close";["date"]="Wed, 15 Mar 2017 19:31:24 GMT";["location"]="S%C3%A9ance";}
body is ""
equal
HTTP/1.1 301 Moved Permanently - GET was for "https://www.example.com/S%C3%A9ance"
如何能够使用lua以及尽可能少的其他依赖项来检索难以捉摸的页面?
答案 0 :(得分:0)
看起来很明显,或许请求的网址与实际位置不同。
如果您遇到类似问题,请在外部库中进行深入检查,以确保他们按照您的想法执行操作。
在这种情况下,luasocket执行urldecode然后urlencode url,因此最终请求不是它的样子。