码迷,mamicode.com
首页 > Web开发 > 详细

htmlunit 校验验证码

时间:2017-06-07 15:37:20      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:pat   contain   password   .sh   pwd   image   ring   des   val   

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());

 

htmlunit 校验验证码

标签:pat   contain   password   .sh   pwd   image   ring   des   val   

原文地址:http://www.cnblogs.com/cmyxn/p/6957045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!