Superagent Version: 3.8.3
Sample code:
varProcess= require(' Process ');varSuperagent= require(' superagent ');varhttp= require(' http ');varFs= require(' FS ');varReadLine= require(' ReadLine ');varPicurl= "Http://192.168.1.125:9080/acps-mgm/login/changeValidateCode";varPostURL= "Http://192.168.1.125:9080/acps-mgm/login/login";varTesturl= "http://192.168.1.125:9080/acps-mgm/merchant/view/235162";varUsername= "username";varPassword= "E99a18c428cb38d5f260853678922e03"; //"abc123" after encryptionvarInitcookie= "Jsessionid=00000twi_3hqfk__rhfrohzqjiu:-1";varOptions= { 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 ');varReqforpic= http.Request(Options, function(RES){ Console.Log("Reqforpic:status =" + Res.Statescode); Console.Log(' reqforpic:headers = ' + JSON.stringify(Res.Headers)); varPiccookie= Res.Headers[' Set-cookie '][0].Split('; ')[0]; Console.Log("pic Cookie:" +Piccookie); Res.setencoding(' binary '); varContent= ""; Res. on(' Data ',Data=Content+=Data). on(' End ', function(){ FS.Writefilesync(__dirname+ '/valicode.jpg ',Content, ' binary '); Console.Log(' successfully downloaded verification code '); varRl= ReadLine.Createinterface({ input: Process.stdin, Output: Process.stdout }); RL.question("Please enter a verification code?" ",Answer= { varValicode=Answer; RL.Close(); Console.Log("The Verification code entered is:" +Valicode); //Post Section 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", " the") .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("Error:" +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("Error 2:" +ERR2); } Else { Console.Log(Res2.text); } }); } }); }); });});Reqforpic. on(' ERROR ',Err= Console.Log("An error occurred while requesting the verification code:" + Err.message));Reqforpic.End();
node. JS uses superagent to implement the analog login function (including the Download Verification code feature)