firefox 和 luasocket 的区别headers

Difference between firefox and luasocket with same headers

我正在尝试通过 luasocket 获取“https://translate.google.com”。

Headers 基于 HttpFox 的输出;

我尝试获取内容:

local r, c, h, fc = http.request { -- result (1 or nil on error), code (should be 200), headers, fancy code
    url = 'http://translate.google.com';
    method = 'GET';
    sink = sink;
    headers = {
        ['User-Agent'] = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0';
        ['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
        ['Host'] = 'translate.google.com';
        ['Accept-Encoding'] = 'gzip, deflate, br';
        ['Accept-Language'] = 'ru,ru-RU;q=0.8,en;q=0.5,en-US;q=0.3';
        ['DNT'] = '1';
        ['Upgrade-Insecure-Requests'] = '1';
        ['Connection'] = 'close';
    };
}

sink 是有效的 ltn12 接收器;

结果:

Code: HTTP/1.1 302 Found
Headers: {
  content-type : "text/html; charset=UTF-8"
  connection : "close"
  content-length : "226"
  x-xss-protection : "1; mode=block"
  p3p : "CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info.""
  server : "HTTP server (unknown)"
  cache-control : "private, max-age=0"
  content-language : "ru"
  date : "Tue, 03 Oct 2017 13:50:21 GMT"
  set-cookie : "NID=113=CO3BdznV6UYwcIZoIdF9F7dW1Cooi5ZVmNML0cQI6kA_TvfNig8xRQgS5E9dSKnIZxcfR3jxUbo3RA-7AEsqOCakciOo7Swtrcvz70Cmpm5M-_m1UQYTBBCg8VyNxzBW; expires=Wed, 04-Apr-2018 13:50:21 GMT; path=/; domain=.google.com; HttpOnly"
  x-content-type-options : "nosniff"
  expires : "Tue, 03 Oct 2017 13:50:21 GMT"
  location : "https://translate.google.com/"
}
Body: 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://translate.google.com/">here</A>.
</BODY></HTML>

我的请求有什么问题?为什么 firefox 有正确的页面而我没有?

Code: HTTP/1.1 302 Found
location : "https://translate.google.com/"

这是重定向到 HTTPS 位置,但是 luasocket 没有 "know" 如何处理 https,所以它不遵循重定向。如果您安装 luasec 并将 local http = require "socket.http" 替换为 local http = require "ssl.https",您应该得到如下内容:

1   200 table: 0x000274e0   HTTP/1.1 200 OK