|
吾爱游客
发表于 2018-2-10 14:46
1、申请会员ID:Keep(或keep123)
2、个人邮箱:923823264@qq.com
3、原创技术文章:我目前上大二,课余时间充足,希望到贵站学习更多知识。下面附上一篇模拟登录校教务系统的教程:
|
- Jsoup应该是Java最简单的网页解析框架
- 下载地址:https://jsoup.org/download
- 该实例以强智教务系统为例:http://jwxt.qlu.edu.cn
- 该教务系统带有验证码,我们就先获取验证码,从Chrome工具中得到验证码地址,如图:
- 分析Post登录参数,我们发现,参数中并没有我们提交的用户名,密码之类的参数,反而只有形如:
view:
1
useDogCode:
encoded:
2i50E6o109c1c24013L1286734P4HF%6mc%n%00D38232529
RANDOMCODE:
1zbz- IDA Console, Courier New, monospace">这样的参数。
- 于是我们回到网页表单中,找到对应的表单:
-
- 然后找到对应的方法:
我们得到了它的运行逻辑,再写代码就容易多了。看代码: package cn.zyzpp.eduCookie;import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.Scanner;import org.jsoup.Connection;import org.jsoup.Connection.Method;import org.jsoup.Connection.Response;import org.jsoup.Jsoup;import org.junit.Before;import org.junit.Test;import cn.zyzpp.eduCookie2.S;/** * 模拟登录带验证码的教务系统 * * 2018-2-9 */public class JsoupSafeCode { private String url_safecode = "http://jwxt.qlu.edu.cn/verifycode.servlet?t=0.020974584"; // 验证码 private String url_encode = "http://jwxt.qlu.edu.cn/Logon.do?method=logon&flag=sess"; // 加密字符串 private String url_Login = "http://jwxt.qlu.edu.cn/Logon.do?method=logon"; // 登录 private String username = ""; private String password = ""; private String path = JsoupSafeCode.class.getResource("/").getPath().replaceAll("%20", " ") + "safecode.png"; private Map<String, String> cookie; /** * 下载验证码 * 保存Cookie * @throws IOException */ public void getSafeCode() throws IOException { Response response = Jsoup.connect(url_safecode).ignoreContentType(true) // 获取图片需设置忽略内容类型 .userAgent("Mozilla").method(Method.GET).timeout(3000).execute(); cookie = response.cookies(); byte[] bytes = response.bodyAsBytes(); Util.saveFile(path, bytes); System.out.println("保存验证码到:" + path); } /** * 登录教务系统 */ public void initLogin() throws IOException { S.print("输入验证码:"); Scanner scan = new Scanner(System.in); String code = scan.next(); try { Map<String, String> data = new HashMap<String, String>(); data.put("view", "1"); data.put("encoded", getEncoded()); data.put("RANDOMCODE", code); Connection connect = Jsoup.connect(url_Login) .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") .userAgent("Mozilla").method(Method.POST).data(data).timeout(3000); for (Map.Entry<String, String> entry : cookie.entrySet()) { connect.cookie(entry.getKey(), entry.getValue()); } Response response = connect.execute(); S.println(response.parse().text().toString()); } catch (IOException e) { } } /** * 加密参数(依具体环境而定,加密算法一般在JS中获得) */ public String getEncoded() { try { Connection connect = Jsoup.connect(url_encode) .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") .userAgent("Mozilla").method(Method.POST).timeout(3000); for (Map.Entry<String, String> entry : cookie.entrySet()) { connect.cookie(entry.getKey(), entry.getValue()); } Response response = connect.execute(); String dataStr = response.parse().text(); // 加密encoded: String scode = dataStr.split("#")[0]; String sxh = dataStr.split("#")[1]; String code = username + "%%%" + password; String encoded = ""; for (int i = 0; i < code.length(); i++) { if (i < 20) { encoded = encoded + code.substring(i, i + 1) + scode.substring(0, Integer.parseInt(sxh.substring(i, i + 1))); scode = scode.substring(Integer.parseInt(sxh.substring(i, i + 1)), scode.length()); } else { encoded = encoded + code.substring(i, code.length()); i = code.length(); } } return encoded; } catch (IOException e) { } return null; }}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
package cn.zyzpp.eduCookie;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;public class Util { /** * 将字节流转换成文件 * * @param filename * @param data * @throws Exception */ public static void saveFile(String filename, byte[] data) { if (data != null) { String filepath = filename; File file = new File(filepath); if (file.exists()) { file.delete(); } try { FileOutputStream fos = new FileOutputStream(file); fos.write(data, 0, data.length); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } }} |
-
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|