如果网站不使用HTTP基本身份验证,但使用HTML表单autentificate用户,你必须到现场开发商没有进入,最好的办法弄清楚是怎么回事是什么浏览器来偷看确实。
启动您的Firebug或Google Chrome Developer Tools或某个HTTP调试代理。
在浏览器中打开该网站,登录并查看浏览器做了什么请求,以及该网站的回复是什么。你必须在你的程序中模仿相同的请求。
请注意,很可能该网站将要求您在随后的请求中发送会话信息以保持身份验证。它可能是一个cookie(或几个)和/或一个GET参数。再次看看浏览器的功能和模仿。
至于格式 - 在网上搜索例子,有几个。
更新:好的,这里是一个例子。
请注意,示例中使用的网址即将过期。只需在http://requestb.in/创建您自己的。在浏览器中打开http://requestb.in/vbpkxivb?inspect,查看程序发送的数据。不要发送真正的登录名和密码到这项服务!
require 'socket.http'
local request_body = [[login=user&password=123]]
local response_body = { }
local res, code, response_headers = socket.http.request
{
url = "http://requestb.in/vbpkxivb";
method = "POST";
headers =
{
["Content-Type"] = "application/x-www-form-urlencoded";
["Content-Length"] = #request_body;
};
source = ltn12.source.string(request_body);
sink = ltn12.sink.table(response_body);
}
print("Status:", res and "OK" or "FAILED")
print("HTTP code:", code)
print("Response headers:")
if type(response_headers) == "table" then
for k, v in pairs(response_headers) do
print(k, ":", v)
end
else
-- Would be nil, if there is an error
print("Not a table:", type(response_headers))
end
print("Response body:")
if type(response_body) == "table" then
print(table.concat(response_body))
else
-- Would be nil, if there is an error
print("Not a table:", type(response_body))
end
print("Done dumping response")
预期输出:
Status: OK
HTTP code: 200
Response headers:
date : Sat, 23 Jun 2012 07:49:13 GMT
content-type : text/html; charset=utf-8
connection : Close
content-length : 3
Response body:
ok
Done dumping response
你尝试过这么远吗?检查[luasocket标签]中的其他问题(http://stackoverflow.com/questions/tagged/luasocket) – hjpotter92
感谢您的建议。 – ms2008