当前位置:网站首页>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))
}
});
}边栏推荐
- On the light application platform finclip and the mobile application development platform mpaas
- The rtsp/onvif protocol video platform easynvr startup service reports an error "service not found". How to solve it?
- 4- draw ellipse, use timer
- When easynvr service is started, video cannot be played due to anti-virus software interception. How to deal with it?
- Hongmeng reads the resource file
- Subsets of leetcode topic resolution
- Leetcode topic analysis sort colors
- Batch generation of code128- C barcode
- 986. Interval List Intersections
- 438. Find All Anagrams in a String
猜你喜欢

The rtsp/onvif protocol video platform easynvr startup service reports an error "service not found". How to solve it?

173. Binary Search Tree Iterator

3. caller service call - dapr

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

点云库pcl从入门到精通 第十章

Summary of communication mode and detailed explanation of I2C drive

Why do we say that the data service API is the standard configuration of the data midrange?

The most commonly used 5-stream ETL mode

Geoserver添加mongoDB数据源

高通9x07两种启动模式
随机推荐
[QNX Hypervisor 2.2用户手册]6.2 网络
Why is the easycvr Video Fusion platform offline when cascading with the Hikvision platform? How to solve it?
单编内核驱动模块
Leetcode topic analysis h-index II
Derivation and loading of the trained random forest model
Introduction to typescript and basic types of variable definitions
173. Binary Search Tree Iterator
Open source stealing malware mercurial found in the field for "educational purposes"
Restore the default routing settings of the primary network card
谈谈 @Autowired 的实现原理
Leetcode topic analysis 3sum
Implementing an open source app store with swiftui
173. Binary Search Tree Iterator
史上最污技术解读,60 个 IT 术语我居然秒懂了......
Happy number of leetcode topic analysis
usb peripheral 驱动 - configfs
MQTT+Flink实现实时消息的订阅与发布
GeoServer adding mongodb data source
Leetcode topic analysis spiral matrix II
node request模块cookie使用