当前位置:网站首页>Node request module cookie usage
Node request module cookie usage
2022-06-23 08:49:00 【An_ s】
Preface
Many websites have 302 perhaps 301 Imitative reptile , The response header will take set-cookie, Then we use node How to deal with it ?
Start
Concise Edition
let request = require("request");
// Open record cookie, Automatically bring it when redirecting cookie
request = request.defaults({jar: true});
function login_redirect(url) {
console.log("url: ", url);
let options = {
url: url,
method: "GET",
headers: {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.62 Safari/537.36'
},
// Redirect
followRedirect: false
}
request(options, function(error, response, body) {
console.log("...login_redirect.....");
// console.log(response.statusCode);
// console.log(response.headers);
if (response.statusCode == 302 || response.statusCode == 301) {
login_redirect(response.headers.location);
} else {
console.log("... Jump over ...");
// console.log(response);
// console.log(body);
}
});
}File version
let request = require("request");
// Created in the root directory cookie.json file
const FileCookieStore = require('tough-cookie-filestore');
const cookieJsonStore = new FileCookieStore('cookies.json')
const j = request.jar(cookieJsonStore);
request = request.defaults({ jar : j })
function login_redirect(url) {
console.log("url: ", url);
let options = {
url: url,
method: "GET",
headers: {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.62 Safari/537.36'
},
// Redirect
followRedirect: false
}
request(options, function(error, response, body) {
console.log("...login_redirect.....");
// console.log(response.statusCode);
// console.log(response.headers);
if (response.statusCode == 302 || response.statusCode == 301) {
login_redirect(response.headers.location);
} else {
console.log("... Jump over ...");
// console.log(response);
// console.log(body);
}
});
}Recommended version
let request = require("request");
const j = request.jar();
request = request.defaults({jar:j});
function login_redirect(url) {
console.log("url: ", url);
let options = {
url: url,
method: "GET",
headers: {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.62 Safari/537.36'
},
followRedirect: false
}
request(options, function(error, response, body) {
console.log("...login_redirect.....");
// console.log(response.statusCode);
// console.log(response.headers);
if (response.statusCode == 302 || response.statusCode == 301) {
login_redirect(response.headers.location);
} else {
console.log("... Jump over ...");
// console.log(response);
// console.log(body);
console.log("cookie by :", j.getCookieString(url))
}
});
}边栏推荐
- Interpretation of the most dirty technology in history, I can understand 60 it terms in seconds
- Talk about the implementation principle of @autowired
- '教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
- Subsets II of leetcode topic analysis
- Map interface and its sub implementation classes
- MQTT+Flink实现实时消息的订阅与发布
- 1、 Software architecture evaluation
- Open source technology exchange batch stream integrated data synchronization engine Chunjun data restore DDL function module analysis
- Monitor the cache update of Eureka client
- 谈谈 @Autowired 的实现原理
猜你喜欢

Which is better, semrush or ahrefs? Which is more suitable for GoogleSEO keyword analysis

'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册

297. Serialize and Deserialize Binary Tree

What are the PCB characteristics inspection items?

3. Caller 服务调用 - dapr

In June, China database industry analysis report was released! Smart wind, train storage and regeneration

The most commonly used 5-stream ETL mode

GeoServer adding mongodb data source

谈谈 @Autowired 的实现原理

The fourth online workshop review
随机推荐
Flutter achieves the effect of selecting seats in the cinema!
Paper reading [quovadis, action recognition? A new model and the dynamics dataset]
Fraction to recursing decimal
Leetcode topic analysis h-index II
New engine, new capability, new experience, Tencent host security flagship release
Use newbeecoder UI implements data paging
[operating steps] how to set the easynvr hardware device to be powered on without automatic startup?
How can I handle the "unable to load" exception when easyplayer plays webrtcs?
670. Maximum Swap
Qualcomm 9x07 two startup modes
[qnx hypervisor 2.2 user manual]6.2 network
Summary of communication mode and detailed explanation of I2C drive
node request模块cookie使用
636. Exclusive Time of Functions
Open source technology exchange batch stream integrated data synchronization engine Chunjun data restore DDL function module analysis
Batch generation of code128- C barcode
驱动架构 & platform平台总线驱动模型
[QNX Hypervisor 2.2用户手册]6.1 使用QNX Hypervisor系统
Interpretation of the most dirty technology in history, I can understand 60 it terms in seconds
On the light application platform finclip and the mobile application development platform mpaas