jsoup simulation landing information portal of Hefei University of Technology
Website: Login Interface of Information Portal of Hefei University of Technology
Open the F12 debugging console, after successful login, found that the login process jumped twice, requesting two files (excluding the original login interface files)
I. login.portal file
- Note that this is a login with authentication code.
- This file returns two cookies, JSESSIONID and cookie, but in fact the two cookies are returned by the authentication code, mainly used to represent the authentication code and view the authentication code picture. Verification Code Picture Link You can see that the two cookie s are returned at the time of access.
- If we want to simulate successful landing, we have to get these two cookie s, and at the same time we have to be able to get the authentication code.
- Here, I use tess4j to identify the letters on the authentication code picture. I use maven and just add dependencies.`
<dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>3.2.1</version> </dependency>
- tess4j also needs to install the training set, because the validation code is alphanumeric, and I use eng.traineddata in English. Download link,
Put it in the tessdata directory (this directory is best placed in the project root directory, so there is no need for additional configuration). For specific use, there will be code below.
2. userPasswordValidate.portal file
- Verify that the username and password are correct, and if they are correct, return the cookie whose key is the iPlanet Directory Pro.
- You can see that requesting this file requires passing in two cookies, JSESSIONID and cookie, and you need to carry the following data.
III. index.portal file
- You need to pass in three cookies, JSESSIONID, cookie, and iPlanet Directory Pro.
Code
Complete code Full code links
/* Save cookie values */ Map<String, String> cookies = new HashMap<>(); /* Load the validation code picture, get the cookie, and parse the validation code */ Connection connection = Jsoup.connect("http://my.hfut.edu.cn/captchaGenerate.portal?s=0.2679940000310119"); /* Construct the request header */ connection.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"); connection.header("Accept-Encoding", "gzip, deflate"); connection.header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8"); connection.header("Cache-Control", "no-cache"); connection.header("Connection", "keep-alive"); connection.header("Host", "my.hfut.edu.cn"); connection.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like " + "Gecko) Chrome/76.0.3809.100 Safari/537.36"); Connection.Response response = null; /* Validation code value */ String captcha = ""; try { /* Request content must be ignored to obtain authentication code picture */ response = connection.ignoreContentType(true).method(Connection.Method.GET).execute(); /* Store the acquired cookie in the hash table defined above */ cookies.putAll(response.cookies()); /* Converting images to buffered images * Using tesseract to analyze pictures * */ ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.bodyAsBytes()); BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream); Tesseract tesseract = new Tesseract(); captcha = tesseract.doOCR(bufferedImage).substring(0, 4); } catch (Exception e) { e.printStackTrace(); } /* Enter your username */ String username = "xxxxxxxxxx"; /* Enter your password */ String password = "xxxxxx"; /* data */ Map<String, String> datas = new HashMap<>(); datas.put("Login.Token1", username); datas.put("Login.Token2", password); datas.put("captchaField", captcha); datas.put("goto", "http://my.hfut.edu.cn/loginSuccess.portal"); datas.put("gotoOnFail", "http://my.hfut.edu.cn/loginFailure.portal"); /* Access the interface for validating usernames and passwords */ Connection connection1 = Jsoup.connect("http://my.hfut.edu.cn/userPasswordValidate.portal"); connection1.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng," + "*/*;q=0.8,application/signed-exchange;v=b3"); connection1.header("Accept-Encoding", "gzip, deflate"); connection1.header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8"); connection1.header("Cache-Control", "no-cache"); connection1.header("Connection", "keep-alive"); connection1.header("Content-Type", "application/x-www-form-urlencoded"); connection1.header("Referer", "http://my.hfut.edu.cn/index.portal"); connection1.header("Host", "my.hfut.edu.cn"); connection1.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like " + "Gecko) Chrome/76.0.3809.100 Safari/537.36"); /* Carry cookie s */ connection1.cookies(cookies); /* Carry data */ connection1.data(datas); Connection.Response response1 = null; try { response1 = connection1.method(Connection.Method.POST).execute(); cookies.putAll(response1.cookies()); } catch (Exception e) { e.printStackTrace(); } /* Resolve the interface after successful login */ Connection connection2 = Jsoup.connect("http://my.hfut.edu.cn/index.portal"); connection2.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng," + "*/*;q=0.8,application/signed-exchange;v=b3"); connection2.header("Accept-Encoding", "gzip, deflate"); connection2.header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8"); connection2.header("Cache-Control", "no-cache"); connection2.header("Connection", "keep-alive"); connection2.header("Content-Type", "application/x-www-form-urlencoded"); connection2.header("Referer", "http://my.hfut.edu.cn/index.portal"); connection2.header("Host", "my.hfut.edu.cn"); connection2.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like " + "Gecko) Chrome/76.0.3809.100 Safari/537.36"); /* Carry cookie s */ connection2.cookies(cookies); Connection.Response response2 = null; try { response2 = connection2.method(Connection.Method.GET).execute(); } catch (Exception e) { e.printStackTrace(); }