欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

node.js使用superagent实现模拟登陆功能(包含下载验证码功能)

程序员文章站 2022-04-18 09:29:28
...

superagent版本:3.8.3

样例代码:

var process = require('process');
var superagent = require('superagent');
var http = require('http');
var fs = require('fs');
var readline = require('readline');

var picUrl = "http://192.168.1.125:9080/acps-mgm/login/changeValidateCode";
var postUrl = "http://192.168.1.125:9080/acps-mgm/login/login";
var testUrl = "http://192.168.1.125:9080/acps-mgm/merchant/view/235162";
var username = "username";
var password = "e99a18c428cb38d5f260853678922e03"; // "abc123"加密后
var initCookie = "JSESSIONID=00000twI_3HQFk__rHfRohzQJiU:-1";

var options = {
    hostname: "192.168.1.125",
    port: 9080,
    path: "/acps-mgm/login/changeValidateCode",
    method: "GET",
    headers: {
        'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'Cookie': initCookie,
        'Host': '192.168.1.125:9080',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36'
    }
}
console.log('111');
var reqForPic = http.request(options, function (res) {
    console.log("ReqForPic: status = " + res.statesCode);
    console.log('ReqForPic: headers = ' + JSON.stringify(res.headers));

    var picCookie = res.headers['set-cookie'][0].split(';')[0];
    console.log("pic cookie: " + picCookie);
    res.setEncoding('binary');
    var content = "";
    res.on('data', data => content += data)
        .on('end', function () {
            fs.writeFileSync(__dirname + '/valiCode.jpg', content, 'binary');
            console.log('成功下载验证码');
            var rl = readline.createInterface({
                input: process.stdin,
                output: process.stdout
            });
            rl.question("请输入验证码?", answer => {
                var valiCode = answer;
                rl.close();
                console.log("输入的验证码是:" + valiCode);

                // post部分
                superagent.post(postUrl)
                    .type("form")
                    .set("Accept", "application/json, text/javascript, */*; q=0.01")
                    .set("Accept-Encoding", "gzip, deflate")
                    .set("Accept-Language", "zh-CN,zh;q=0.9")
                    .set("Connection", "keep-alive")
                    .set("Content-Length", "72")
                    .set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                    .set("Cookie", picCookie)
                    .set("Host", "192.168.1.125:9080")
                    .set("Origin", "http://192.168.1.125:9080")
                    .set("Referer", "http://192.168.1.125:9080/acps-mgm/login.html")
                    .set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36")
                    .set("X-Requested-With", "XMLHttpRequest")
                    .send({
                        password: password,
                        username: username,
                        vailCode: valiCode
                    })
                    .end( function (err, res) {
                        
                        if (err) {
                            console.log("出错:" + err);
                        } else {
                            console.log(res.text);

                            superagent.get(testUrl)
                                .set("Accept", "*/*")
                                .set("Accept-Encoding", "gzip, deflate")
                                .set("Accept-Language", "zh-CN,zh;q=0.9")
                                .set("Connection", "keep-alive")
                                .set("Cookie", picCookie)
                                .set("Host", "192.168.1.125:9080")
                                .set("Referer", "http://192.168.1.125:9080/acps-mgm/")
                                .set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36")
                                .end( (err2, res2)=> {
                                    if (err2) {
                                        console.log("出错2:" + err2);
                                    } else {
                                        console.log(res2.text);
                                    }
                                } );
                        }

                    } );

            });
        });
});

reqForPic.on('error', err => console.log("请求验证码的时候发生错误:" + err.message));

reqForPic.end();