如何在 NodeJS 中维护请求会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19936705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:16:00  来源:igfitidea点击:

How to maintain a request session in NodeJS

javascriptnode.jssessionrequest

提问by Ryan

I'm trying to use NodeJSto scrape a website that requires a login by POST. Then once I'm logged in I can access a separate webpage by GET.

我试图用来NodeJS抓取一个需要登录的网站POST。然后,一旦我登录,我就可以通过GET.

The first problem right now is logging in. I've tried to use requestto POSTthe login information, but the response I get does not appear to be logged in.

第一个问题,现在被登录。我试图使用requestPOST的登录信息,但我得到的回应没有出现在被记录。

exports.getstats = function (req, res) {
    request.post({url : requesturl, form: lform}, function(err, response, body) {
        res.writeHeader(200, {"Content-Type": "text/html"});
        res.write(body);
        res.end();
    });
};

Here I'm just forwarding the page I get back, but the page I get back still shows the login form, and if I try to access another page it says I'm not logged in.

在这里,我只是转发我返回的页面,但我返回的页面仍然显示登录表单,如果我尝试访问另一个页面,它会说我没有登录。

I think I need to maintain the client side session and cookie data, but I can find no resources to help me understand how to do that.

我想我需要维护客户端会话和 cookie 数据,但是我找不到任何资源来帮助我理解如何做到这一点。



As a followup I ended up using zombiejsto get the functionality I needed

作为后续,我最终使用了zombiejs来获得我需要的功能

回答by Peter Lyons

You need to make a cookie jar and use the same jar for all related requests.

您需要制作一个 cookie jar 并对所有相关请求使用相同的 jar。

 var cookieJar = request.jar();
 request.post({url : requesturl, jar: cookieJar, form: lform}, ...

That should in theory allow you to scrape pages with GET as a logged-in user, but only once you get the actual login code working. Based on your description of the response to your login POST, that may not be actually working correctly yet, so the cookie jar won't help until you fix the problems in your login code first.

从理论上讲,这应该允许您以登录用户的身份使用 GET 抓取页面,但只有在您使实际登录代码正常工作后才能使用。根据您对登录 POST 响应的描述,这可能实际上尚未正常工作,因此在您首先修复登录代码中的问题之前,cookie jar 将无济于事。

回答by Henrique Rotava

The request.jar();didn't work for me. So I am using the headers response to make another request like this:

request.jar();没有对我来说有效。所以我使用 headers 响应来发出另一个这样的请求:

request.post({
    url: 'https://exampleurl.com/login',
    form: {"login":"xxxx", "password":"xxxx"}
}, function(error, response, body){

    request.get({
        url:"https://exampleurl.com/logged",
        header: response.headers
    },function(error, response, body){
        // The full html of the authenticated page
        console.log(body);
    });
});

Actualy this way is working fine. =D

实际上这种方式工作正常。=D

回答by Steve

Request manages cookies between requests if you enable it:

如果您启用它,请求会管理请求之间的 cookie:

Cookies are disabled by default (else, they would be used in subsequent requests). To enable cookies, set jar to true (either in defaults or options).

默认情况下禁用 Cookie(否则,它们将在后续请求中使用)。要启用 cookie,请将 jar 设置为 true(在默认值或选项中)。

const request = request.defaults({jar: true})
request('http://www.google.com', function () {
  request('http://images.google.com')
});