gpt4 book ai didi

javascript - 如何使用 Request 和 NodeJS 到达重定向页面

转载 作者:行者123 更新时间:2023-11-30 00:33:45 25 4
gpt4 key购买 nike

我正在使用 Request、Jsdom 和 NodeJS 登录网页。根据Request api ,

followRedirect - 遵循 HTTP 3xx 响应作为重定向(默认值:true)。此属性也可以作为函数实现,该函数将响应对象作为单个参数获取,如果重定向应该继续,则返回 true,否则返回 false。

鉴于这默认为 true,我认为该请求会给我重定向的页面,而不是中间的重定向页面。

我的代码:

    request(loginPageURL, function(error, response, body) {
/*Error handling removed for brevity*/
jsdom.env({
html: body,
scripts: [
'http://code.jquery.com/jquery-1.7.1.min.js'
],
done: function(err, window) {
//Set login fields
var form = $('#authorizationForm');
var data = form.serialize();
var url = form.attr('action') || 'get';
var type = form.attr('enctype') || 'application/x-www-form-urlencoded';
var method = form.attr('method');

request({
url: url,
method: method.toUpperCase(),
body: data,
headers: {
'Content-type': type
}
}, function(error, response, body) {
if (!error) {
console.log("Response.statusCode:" + response.statusCode);
console.log("Body:" + body);

登录后没有给我主页,输出是:

Response.statusCode: 302
Body: To access this site, go to <a href="...">

虽然从重定向页面获取代码可能很有用,但如何跳转到它重定向到的页面?

最佳答案

是非GET请求吗?然后您需要显式设置 followAllRedirects: true

followAllRedirects - follow non-GET HTTP 3xx responses as redirects (default: false)

https://github.com/request/request

(followRedirect 的文档令人困惑,因为它们似乎没有表明 followRedirect 仅适用于 GET。)

关于javascript - 如何使用 Request 和 NodeJS 到达重定向页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28159580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com