js 中 fetch 跟踪连续跳转的网址
let session_cookie = login_data_response.headers.get("Set-Cookie") || ""
// allow_redirects 设置为 False,避免重定向请求
let response = await fetch(redirect_url, {
method: "GET",
headers: {
Cookie: session_cookie,
},
redirect: "manual",
})
session_cookie = response.headers.get("Set-Cookie") || ""
// console.log(`session_cookie: ${session_cookie}`)
// 处理多级 302 跳转
while (response.status === 302) {
// 获取当前响应的 URL
const current_url = response.headers.get("Location") || ""
if (!current_url) {
break
}
// console.log(current_url);
// 发送下一次跳转请求,继续禁用自动跟踪重定向
response = await fetch(current_url, {
method: "GET",
headers: {
Cookie: session_cookie,
},
redirect: "manual",
})
const current_cookie = response.headers.get("Set-Cookie")
if (current_cookie) {
session_cookie = current_cookie
}
}