问题:
// This is what I tried.
// Headers are copied from my website's actual requests via Chrome Dev Tool's network tab.
const https = require('https')
const options = {
host: 'api.com',
port: 443,
path: '/form',
method: 'POST',
headers: {
Accept: '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8,de;q=0.7',
Connection: 'keep-alive',
'Content-Length': '127',
'Content-Type': 'application/x-www-form-urlencoded',
Host: 'api.com',
Origin: 'https://www.mywebsite.com',
Referer: 'https://www.mywebsite.com/',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
},
form: {
name: 'My Name'
}
};
const req = https.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
})
req.end();
它们主要基于 TLS Fingerprinting 阻止来自服务器、机器人或抓取器的请求。它检测请求是否来自真实的浏览器
curl-impersonate:可以模拟 Chrome 和 Firefox 的特殊版本的 curl 修复了此问题