htmlUnit 校验验证码
直接上代码
1 String url = "http://www.zycg.gov.cn/"; 2 3 WebclientUtil webClientUtils = new WebclientUtil(); 4 WebClient webClient = webClientUtils.getWebClient(); 5 6 HtmlPage page = webClient.getPage(url); 7 8 HtmlElement username = page.getFirstByXPath("//*[@id=‘u_name‘]"); 9 HtmlElement password = page.getFirstByXPath("//*[@id=‘u_pwd1‘]"); 10 HtmlElement valiCode = page.getFirstByXPath("//*[@id=‘yzm‘]"); 11 HtmlImage valiCodeImg = (HtmlImage) page.getFirstByXPath("//*[@id=‘img_random‘]"); 12 ImageReader imageReader = valiCodeImg.getImageReader(); 13 BufferedImage bufferedImage = imageReader.read(0); 14 15 JFrame f2 = new JFrame(); 16 JLabel l = new JLabel(); 17 l.setIcon(new ImageIcon(bufferedImage)); 18 f2.getContentPane().add(l); 19 f2.setSize(100, 100); 20 f2.setTitle("验证码"); 21 f2.setVisible(true); 22 23 String valicodeStr = JOptionPane.showInputDialog("请输入验证码:"); 24 f2.setVisible(false); 25 HtmlForm submit = page.getFirstByXPath("//*[@id=‘submitForm‘]"); 26 27 username.click(); 28 username.type("zhongkeruan"); 29 password.click(); 30 password.type("62570007"); 31 valiCode.click(); 32 valiCode.type(valicodeStr); 33 34 35 ScriptResult result = page.executeJavaScript("dl()"); 36 page = (HtmlPage) result.getNewPage(); 37 38 if(page.asText().contains("下一页")){ 39 System.out.println("登录成功"); 40 }else{ 41 System.out.println("登录失败"); 42 } 43 System.out.println(page.asText());
时间: 2024-11-05 11:53:20