Luasocket自定义标题,404变成301。

我的上一个问题是关于如何使用 socket.http 模块在 lua 中获取网页标题的。问题在这里。之前,youtube 页面导致我遇到了 404 错误页面。根据 MattJ 的帮助,我为请求设置了自定义的 HOST 标头。这是我所做的和结果:

代码

header = { host= "youtube.com" }
local result,b,c,h = http.request{ url = "http://www.youtube.com/watch?v=_eT40eV7OiI", headers = header }
print ( result, b, c, h )
for k,v in pairs(c) do print(k,v) end

结果

1   301 table: 0047D430 HTTP/1.1 301 Moved Permanently
x-content-type-options  nosniff
content-length  0
expires Tue, 27 Apr 1971 19:44:06 EST
cache-control   no-cache
connection  close
location    http://www.youtube.com/watch?v=_eT40eV7OiI
content-type    text/html; charset=utf-8
date    Sat, 28 Apr 2012 04:26:21 GMT
server  wiseguy/0.6.11

根据我所能理解的,错误基本上是由于 X-Content-Type-Options 值为 nosniff 导致的。从它的文档中可以得知,唯一定义的值 "nosniff" 可以防止 Internet Explorer 将响应与声明的内容类型不符合的 MIME 进行嗅探。


请帮助我使用自定义代理并从它们的主体中获取 youtube (以及一些在前一个问题中提到的其他网站) 标题。这是我当前拥有的完整 LUA 文件:

local http = require "socket.http"
http.PROXY="http://<proxy address here>:8080"
header = { host= "youtube.com" }
local result,b,c,h = http.request{ url = "http://www.youtube.com/watch?v=_eT40eV7OiI", headers = header }
print ( result, b, c, h )
for k,v in pairs(c) do print(k,v) end

原文链接 https://stackoverflow.com/questions/10360632

点赞
stackoverflow用户415823
stackoverflow用户415823

我认为应该更改这行:

 header = { host= "youtube.com" }

为:

 header = { host= "www.youtube.com" }

这样,我就能用了。

2012-04-28 19:49:06
stackoverflow用户2764551
stackoverflow用户2764551

解决方案是安装luasec,并使用ssl.https模块进行请求。

Paul Kulchenko此处回答!

示例:

-- luasec version 0.4.2
require("ssl")
require("https")
-- ssl.https.request(...)
2016-06-01 18:58:33