网站:合肥工业大学信息门户登录界面
打开F12调试控制台,登录成功后,发现登陆过程一共跳转了两次,请求了两个文件(不包括原来登陆界面的文件)
net.sourceforge.tess4j
tess4j
3.2.1
完整代码 完整代码链接
/* 保存cookie值 */
Map cookies = new HashMap<>();
/* 加载验证码图片,获取cookie,并解析验证码 */
Connection connection = Jsoup.connect("http://my.hfut.edu.cn/captchaGenerate.portal?s=0.2679940000310119");
/* 构造请求头 */
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;
/* 验证码值 */
String captcha = "";
try {
/* 获取验证码图片必须忽略请求内容 */
response = connection.ignoreContentType(true).method(Connection.Method.GET).execute();
/* 将获取的cookie储存在上面定义的哈希表中 */
cookies.putAll(response.cookies());
/* 将图片进行转换成bufferedImage
* 利用tesseract进行图片的解析
* */
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();
}
/* 输入你的用户名 */
String username = "xxxxxxxxxx";
/* 输入你的密码 */
String password = "xxxxxx";
/* 数据 */
Map 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");
/* 访问验证用户名和密码的界面 */
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");
/* 携带cookie */
connection1.cookies(cookies);
/* 携带数据 */
connection1.data(datas);
Connection.Response response1 = null;
try {
response1 = connection1.method(Connection.Method.POST).execute();
cookies.putAll(response1.cookies());
} catch (Exception e) {
e.printStackTrace();
}
/* 解析登录成功后的界面 */
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");
/* 携带cookie */
connection2.cookies(cookies);
Connection.Response response2 = null;
try {
response2 = connection2.method(Connection.Method.GET).execute();
} catch (Exception e) {
e.printStackTrace();
}