luaSocket HTTP 请求总是以重定向(301 或 302)响应

luaSocket HTTP requests always respond with a redirect (301 or 302)

我使用 LuaForWindows(最新版本)并且我已经阅读了 lua-users.org 的 this and this answer and everything i could find in the mailinglist。我尝试过的(大多数)网站只响应 301 或 302。我创建了一个示例批处理脚本,它从他们的手册页下载(一些)OpenGL 2.1 参考。

@ECHO OFF

FOR /F "SKIP=5" %%# IN ( %~fs0 ) DO lua -l socket.http -e "print(socket.http.request('https://www.opengl.org/sdk/docs/man2/xhtml/%%#.xml'))"
GOTO:EOF

glAccum
glActiveTexture
glAlphaFunc
glAreTexturesResident
glArrayElement
glAttachShader
glBegin
glBeginQuery
glBindAttribLocation
glBindBuffer

最重要的部分是:

print(require('socket.http').request('https://www.opengl.org/sdk/docs/man2/xhtml/glAccum.xml')) -- added glAccum so you can run it

这总是 returns 301。从其他随机页面下载时,我也会遇到这种情况。 (我没有注意到它们,所以我不能给出一个列表,但我碰巧发现其中一些使用 cloudflare。)

如果我使用 URL and openConnection() 在 Java 中编写等效的下载程序,它不会重定向。

我已经尝试过手动进行重定向(设置引用程序和内容)并使用 the 'generic' way。正如其他答案中所述的大多数提示。

您正在使用 socket.http,但尝试访问 https URL。 luasocket 不处理 HTTPS 协议,因此它向默认端口 80 发送请求,并获得重定向到 HTTPS link(相同 link);这种情况持续了好几次(因为 URL 并没有真正改变),最后 luasocket 放弃了生成消息。

解决方案是 install luasec 并使用 ssl.https 模块来完成请求。