请求 nodejs 模块不支持某些 url 的重定向

request nodejs module not supporting redirect for some url

我正在使用请求 nodejs 模块为网站获取 html,但不适用于某些重定向网站,如下所示:

var request = require('request');

var options = {
    url: "http://www.amwasia.com",
    headers: {'user-agent': 'node.js'},
    rejectUnauthorized: false,
    followAllRedirects: true
};

request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
       console.log("body>>>>>>"+body)
    } else {
        console.log("error>>>>>>>>>" + error);
        console.log("response statusCode>>>>>>>>>" + response.statusCode);
        console.log("response body>>>>>>>>>" + response.body);
    }
});

这给了我这个输出

正文>>>>>>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.amwmotors.com/">
</head>

<body>
</body>
</html>

这不准确html。

此网站正在重定向到此页面 http://www.amwmotors.com/

我的另一个例子是网站:http://www.pmat.or.th and is redirecting to page http://www.pmat.or.th/main/

这也没有给出正确的输出。

我也试过在选项中没有 followAllRedirects: true

请帮忙?

如果您可以登录您的 body 变量:

if (!error && response.statusCode == 200) {
   console.log("body>>>>>>"+body)
}

,这是因为您收到了 200 状态代码,这不是重定向。如果您通过浏览器测试 url www.amwasia.com 并被重定向,则它是通过 html <meta> 标记完成的:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.amwmotors.com/">

通过请求模块,您似乎得到了 200,而不是 3xx