node.js analog School Dean Login

Just before graduation, so Albert located at, I have a complete set of analog functions I log on to the school's Office of Academic Affairs to obtain cookie, originally I thought it was a very simple function, but it took me two days. (I use the school's Office of Academic Affairs Hunan strong-technology development)

Internet search a large number of simulation login Office of Academic Affairs article, I found that basically java / php / python, actually not a few node.js, I decided to do it yourself.

1, first open the Charles packet capture and complete log in once Dean


Send a lot of requests, it is clear that with a Logon request only the last is the real login to send the request, two requests are POST request, direct look at the body form information and respond to requests


Form data in drawing a black line drawing of me after several retransmissions, found to be useless data, can be deleted directly, that sends the login request as long as USERNAME, PASSWORD and RANDOMCODE. So why two requests diagram?
I found this article

https://www.cnblogs.com/geqianst/p/3286027.html

1 username and password to post two values of HTTP: // 202.114.242.21/whkjdx/Logon.do?method=logon 
2 after operation by 1, the page will jump to HTTP: // 202.114.242.21/whkjdx/ index.jsp, then we post an empty form to http://202.114.242.21/whkjdx/Logon.do?method=logonBySSO, it's only completely successful login, if there is no second step, you will be prompted permission errors

 

I see. Two requests indispensable
2, using the request-promise library Yihuhuhuapiao

 1 // rp-login.js
 2 const rp = require('request-promise')
 3 
 4 const baseUrl = 'http://59.51.24.46/hysf'
 5 let studentCookie = '' // 填入cookie
 6 
 7 const headers = {
 8     Cookie: studentCookie
 9 }
10 
11 const options = {
12     method: 'POST',
13     uri: `${baseUrl}/Logon.do?method=logon`,
14     headers,
15     resolveWithFullResponse: true,
16     form: {
. 17          USERNAME: 'I Account' ,
 18 is          PASSWORD: 'I password' ,
 . 19          RANDOMCODE: '' // fill codes 
20 is      }
 21 is  }
 22 is const optionsSSO = {
 23 is      Method: 'the POST' ,
 24      URI: `$ ? Method /Logon.do} = {the baseUrl logonBySSO`,
 25      headers,
 26 is      resolveWithFullResponse: to true 
27  }
 28  
29  RP (Options)
 30      .then (body => {
 31 is          IF (body.body.includes ( 'main.jsp' )) {
 32             console.log ( 'successful login' )
 33 is          } the else {
 34 is              console.log ( 'login failed' , body)
 35          }
 36  
37 [          RP (optionsSSO)
 38 is              .then (body => {
 39                  console.log ( 'Single Sign success! ' )
 40                  // IF (body.body.includes ( "Menus")) { 
41 is                  // the console.log (' successful login ') 
42 is                  // } the else { 
43 is                  // the console.log (' login failed ' ) 
44                  // } 
45              })
 46              .the catch (ERR => {
 47                  the console.log ( 'single sign-on failed!' , ERR)
 48              })
 49      })
 50      . the catch (ERR => {
 51 is          the console.log ( 'account verification failure or error codes' , ERR)
 52 is      })

 

Open the Registry page


Get codes and cookie, fill in the code, node rp-login.js, no problem, and then into the Office of Academic Affairs home page, enter success, no longer need to check the account
would have thought this over, but I found that in my direct use this code page address in the img tag and then get cookie does not work
http://59.51.24.46/hysf/verifycode.servlet

Because POST requests are sent nodejs will receive a new cookie, and the cookie verification code is not the same, so there is the next step
3, to obtain CAPTCHA image

 1 // rcode.js
 2 const request = require('request')
 3 var fs = require('fs')
 4 
 5 request(
 6     'http://59.51.24.46/hysf/verifycode.servlet',
 7     { encoding: null }, // 此处需设置null,否则获取的body是乱码
 8     (err, res, buffer) => {
 9         if (err) {
10             console.log('出错了', err)
11         } else {
12             const id = res.headers['set-cookie'][0].slice(0, 43)
13             fs.writeFile(`./${id}.jpg`, buffer, err => {
14                 if (err) {
15                     console.log('保存验证码失败:', err)
16                 } else {
17                     console.log('存储验证码图片成功')
18                 }
19             })
20         }
21     }
22 )

 

执行后获得验证码图片和COOKIE

 

 

就是这样,后续的页面处理较简单就不写了

Guess you like

Origin www.cnblogs.com/undezhi/p/11933677.html