node.js - 使用 NodeJS 或其他方式欺骗/检测浏览器请求
问题描述
- 我的网站向第 3 方公共 api 发出 http 发布请求。他们完美地工作。
- 如果我在本地通过 NodeJS 脚本发出相同的请求,我会收到 403 或 500 错误,具体取决于我发送的标头。
问题:
- 服务器能否可靠地检测请求是否来自浏览器?
- 服务器如何检测它?
- 有没有办法用 NodeJS 或其他方法来欺骗浏览器请求?
// 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();
解决方案
推荐阅读
- javascript - 为什么我的函数只在按钮中的 onClick 事件上运行一次?
- java - 如何垂直构造以下 UI 的 RecyclerView?
- java - 如何在 JPA 中删除树状结构中的对象?
- swift - 有没有办法在可扩展单元格的表格视图单元格中放置长文本?(以编程方式)
- php - PHP如何检查变量是否以'pa_'开头?
- excel - 创建具有子级别的类属性
- ruby-on-rails - Heroku 部署错误您必须使用 Bundler 2 或更高版本与此锁定文件
- asp.net-mvc - DropDownList html 助手的 Foreach 具有来自 foreach 本身的值
- javascript - RecordRTC:Ondataavailable 调用了两次。只有第一个文件是正确的,其他文件已损坏或太小
- android - 从活动源文件移动到活动设计器